2 * drivers/base/power/domain.c - Common code related to device power domains.
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 * This file is released under the GPLv2.
9 #include <linux/delay.h>
10 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/pm_domain.h>
15 #include <linux/pm_qos.h>
16 #include <linux/pm_clock.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/sched.h>
20 #include <linux/suspend.h>
21 #include <linux/export.h>
25 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
27 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
29 type (*__routine)(struct device *__d); \
30 type __ret = (type)0; \
32 __routine = genpd->dev_ops.callback; \
34 __ret = __routine(dev); \
39 static LIST_HEAD(gpd_list
);
40 static DEFINE_MUTEX(gpd_list_lock
);
42 struct genpd_lock_ops
{
43 void (*lock
)(struct generic_pm_domain
*genpd
);
44 void (*lock_nested
)(struct generic_pm_domain
*genpd
, int depth
);
45 int (*lock_interruptible
)(struct generic_pm_domain
*genpd
);
46 void (*unlock
)(struct generic_pm_domain
*genpd
);
49 static void genpd_lock_mtx(struct generic_pm_domain
*genpd
)
51 mutex_lock(&genpd
->mlock
);
54 static void genpd_lock_nested_mtx(struct generic_pm_domain
*genpd
,
57 mutex_lock_nested(&genpd
->mlock
, depth
);
60 static int genpd_lock_interruptible_mtx(struct generic_pm_domain
*genpd
)
62 return mutex_lock_interruptible(&genpd
->mlock
);
65 static void genpd_unlock_mtx(struct generic_pm_domain
*genpd
)
67 return mutex_unlock(&genpd
->mlock
);
70 static const struct genpd_lock_ops genpd_mtx_ops
= {
71 .lock
= genpd_lock_mtx
,
72 .lock_nested
= genpd_lock_nested_mtx
,
73 .lock_interruptible
= genpd_lock_interruptible_mtx
,
74 .unlock
= genpd_unlock_mtx
,
77 static void genpd_lock_spin(struct generic_pm_domain
*genpd
)
78 __acquires(&genpd
->slock
)
82 spin_lock_irqsave(&genpd
->slock
, flags
);
83 genpd
->lock_flags
= flags
;
86 static void genpd_lock_nested_spin(struct generic_pm_domain
*genpd
,
88 __acquires(&genpd
->slock
)
92 spin_lock_irqsave_nested(&genpd
->slock
, flags
, depth
);
93 genpd
->lock_flags
= flags
;
96 static int genpd_lock_interruptible_spin(struct generic_pm_domain
*genpd
)
97 __acquires(&genpd
->slock
)
101 spin_lock_irqsave(&genpd
->slock
, flags
);
102 genpd
->lock_flags
= flags
;
106 static void genpd_unlock_spin(struct generic_pm_domain
*genpd
)
107 __releases(&genpd
->slock
)
109 spin_unlock_irqrestore(&genpd
->slock
, genpd
->lock_flags
);
112 static const struct genpd_lock_ops genpd_spin_ops
= {
113 .lock
= genpd_lock_spin
,
114 .lock_nested
= genpd_lock_nested_spin
,
115 .lock_interruptible
= genpd_lock_interruptible_spin
,
116 .unlock
= genpd_unlock_spin
,
119 #define genpd_lock(p) p->lock_ops->lock(p)
120 #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
121 #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
122 #define genpd_unlock(p) p->lock_ops->unlock(p)
124 #define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
125 #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
126 #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
128 static inline bool irq_safe_dev_in_no_sleep_domain(struct device
*dev
,
129 const struct generic_pm_domain
*genpd
)
133 ret
= pm_runtime_is_irq_safe(dev
) && !genpd_is_irq_safe(genpd
);
136 * Warn once if an IRQ safe device is attached to a no sleep domain, as
137 * to indicate a suboptimal configuration for PM. For an always on
138 * domain this isn't case, thus don't warn.
140 if (ret
&& !genpd_is_always_on(genpd
))
141 dev_warn_once(dev
, "PM domain %s will not be powered off\n",
148 * Get the generic PM domain for a particular struct device.
149 * This validates the struct device pointer, the PM domain pointer,
150 * and checks that the PM domain pointer is a real generic PM domain.
151 * Any failure results in NULL being returned.
153 static struct generic_pm_domain
*genpd_lookup_dev(struct device
*dev
)
155 struct generic_pm_domain
*genpd
= NULL
, *gpd
;
157 if (IS_ERR_OR_NULL(dev
) || IS_ERR_OR_NULL(dev
->pm_domain
))
160 mutex_lock(&gpd_list_lock
);
161 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
) {
162 if (&gpd
->domain
== dev
->pm_domain
) {
167 mutex_unlock(&gpd_list_lock
);
173 * This should only be used where we are certain that the pm_domain
174 * attached to the device is a genpd domain.
176 static struct generic_pm_domain
*dev_to_genpd(struct device
*dev
)
178 if (IS_ERR_OR_NULL(dev
->pm_domain
))
179 return ERR_PTR(-EINVAL
);
181 return pd_to_genpd(dev
->pm_domain
);
184 static int genpd_stop_dev(const struct generic_pm_domain
*genpd
,
187 return GENPD_DEV_CALLBACK(genpd
, int, stop
, dev
);
190 static int genpd_start_dev(const struct generic_pm_domain
*genpd
,
193 return GENPD_DEV_CALLBACK(genpd
, int, start
, dev
);
196 static bool genpd_sd_counter_dec(struct generic_pm_domain
*genpd
)
200 if (!WARN_ON(atomic_read(&genpd
->sd_count
) == 0))
201 ret
= !!atomic_dec_and_test(&genpd
->sd_count
);
206 static void genpd_sd_counter_inc(struct generic_pm_domain
*genpd
)
208 atomic_inc(&genpd
->sd_count
);
209 smp_mb__after_atomic();
212 static int _genpd_power_on(struct generic_pm_domain
*genpd
, bool timed
)
214 unsigned int state_idx
= genpd
->state_idx
;
219 if (!genpd
->power_on
)
223 return genpd
->power_on(genpd
);
225 time_start
= ktime_get();
226 ret
= genpd
->power_on(genpd
);
230 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
231 if (elapsed_ns
<= genpd
->states
[state_idx
].power_on_latency_ns
)
234 genpd
->states
[state_idx
].power_on_latency_ns
= elapsed_ns
;
235 genpd
->max_off_time_changed
= true;
236 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
237 genpd
->name
, "on", elapsed_ns
);
242 static int _genpd_power_off(struct generic_pm_domain
*genpd
, bool timed
)
244 unsigned int state_idx
= genpd
->state_idx
;
249 if (!genpd
->power_off
)
253 return genpd
->power_off(genpd
);
255 time_start
= ktime_get();
256 ret
= genpd
->power_off(genpd
);
260 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
261 if (elapsed_ns
<= genpd
->states
[state_idx
].power_off_latency_ns
)
264 genpd
->states
[state_idx
].power_off_latency_ns
= elapsed_ns
;
265 genpd
->max_off_time_changed
= true;
266 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
267 genpd
->name
, "off", elapsed_ns
);
273 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
274 * @genpd: PM domain to power off.
276 * Queue up the execution of genpd_power_off() unless it's already been done
279 static void genpd_queue_power_off_work(struct generic_pm_domain
*genpd
)
281 queue_work(pm_wq
, &genpd
->power_off_work
);
285 * genpd_power_off - Remove power from a given PM domain.
286 * @genpd: PM domain to power down.
287 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
288 * RPM status of the releated device is in an intermediate state, not yet turned
289 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
290 * be RPM_SUSPENDED, while it tries to power off the PM domain.
292 * If all of the @genpd's devices have been suspended and all of its subdomains
293 * have been powered down, remove power from @genpd.
295 static int genpd_power_off(struct generic_pm_domain
*genpd
, bool one_dev_on
,
298 struct pm_domain_data
*pdd
;
299 struct gpd_link
*link
;
300 unsigned int not_suspended
= 0;
303 * Do not try to power off the domain in the following situations:
304 * (1) The domain is already in the "power off" state.
305 * (2) System suspend is in progress.
307 if (!genpd_status_on(genpd
) || genpd
->prepared_count
> 0)
311 * Abort power off for the PM domain in the following situations:
312 * (1) The domain is configured as always on.
313 * (2) When the domain has a subdomain being powered on.
315 if (genpd_is_always_on(genpd
) || atomic_read(&genpd
->sd_count
) > 0)
318 list_for_each_entry(pdd
, &genpd
->dev_list
, list_node
) {
319 enum pm_qos_flags_status stat
;
321 stat
= dev_pm_qos_flags(pdd
->dev
,
322 PM_QOS_FLAG_NO_POWER_OFF
323 | PM_QOS_FLAG_REMOTE_WAKEUP
);
324 if (stat
> PM_QOS_FLAGS_NONE
)
328 * Do not allow PM domain to be powered off, when an IRQ safe
329 * device is part of a non-IRQ safe domain.
331 if (!pm_runtime_suspended(pdd
->dev
) ||
332 irq_safe_dev_in_no_sleep_domain(pdd
->dev
, genpd
))
336 if (not_suspended
> 1 || (not_suspended
== 1 && !one_dev_on
))
339 if (genpd
->gov
&& genpd
->gov
->power_down_ok
) {
340 if (!genpd
->gov
->power_down_ok(&genpd
->domain
))
344 if (genpd
->power_off
) {
347 if (atomic_read(&genpd
->sd_count
) > 0)
351 * If sd_count > 0 at this point, one of the subdomains hasn't
352 * managed to call genpd_power_on() for the master yet after
353 * incrementing it. In that case genpd_power_on() will wait
354 * for us to drop the lock, so we can call .power_off() and let
355 * the genpd_power_on() restore power for us (this shouldn't
356 * happen very often).
358 ret
= _genpd_power_off(genpd
, true);
363 genpd
->status
= GPD_STATE_POWER_OFF
;
365 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
366 genpd_sd_counter_dec(link
->master
);
367 genpd_lock_nested(link
->master
, depth
+ 1);
368 genpd_power_off(link
->master
, false, depth
+ 1);
369 genpd_unlock(link
->master
);
376 * genpd_power_on - Restore power to a given PM domain and its masters.
377 * @genpd: PM domain to power up.
378 * @depth: nesting count for lockdep.
380 * Restore power to @genpd and all of its masters so that it is possible to
381 * resume a device belonging to it.
383 static int genpd_power_on(struct generic_pm_domain
*genpd
, unsigned int depth
)
385 struct gpd_link
*link
;
388 if (genpd_status_on(genpd
))
392 * The list is guaranteed not to change while the loop below is being
393 * executed, unless one of the masters' .power_on() callbacks fiddles
396 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
397 struct generic_pm_domain
*master
= link
->master
;
399 genpd_sd_counter_inc(master
);
401 genpd_lock_nested(master
, depth
+ 1);
402 ret
= genpd_power_on(master
, depth
+ 1);
403 genpd_unlock(master
);
406 genpd_sd_counter_dec(master
);
411 ret
= _genpd_power_on(genpd
, true);
415 genpd
->status
= GPD_STATE_ACTIVE
;
419 list_for_each_entry_continue_reverse(link
,
422 genpd_sd_counter_dec(link
->master
);
423 genpd_lock_nested(link
->master
, depth
+ 1);
424 genpd_power_off(link
->master
, false, depth
+ 1);
425 genpd_unlock(link
->master
);
431 static int genpd_dev_pm_qos_notifier(struct notifier_block
*nb
,
432 unsigned long val
, void *ptr
)
434 struct generic_pm_domain_data
*gpd_data
;
437 gpd_data
= container_of(nb
, struct generic_pm_domain_data
, nb
);
438 dev
= gpd_data
->base
.dev
;
441 struct generic_pm_domain
*genpd
;
442 struct pm_domain_data
*pdd
;
444 spin_lock_irq(&dev
->power
.lock
);
446 pdd
= dev
->power
.subsys_data
?
447 dev
->power
.subsys_data
->domain_data
: NULL
;
449 to_gpd_data(pdd
)->td
.constraint_changed
= true;
450 genpd
= dev_to_genpd(dev
);
452 genpd
= ERR_PTR(-ENODATA
);
455 spin_unlock_irq(&dev
->power
.lock
);
457 if (!IS_ERR(genpd
)) {
459 genpd
->max_off_time_changed
= true;
464 if (!dev
|| dev
->power
.ignore_children
)
472 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
473 * @work: Work structure used for scheduling the execution of this function.
475 static void genpd_power_off_work_fn(struct work_struct
*work
)
477 struct generic_pm_domain
*genpd
;
479 genpd
= container_of(work
, struct generic_pm_domain
, power_off_work
);
482 genpd_power_off(genpd
, false, 0);
487 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
488 * @dev: Device to handle.
490 static int __genpd_runtime_suspend(struct device
*dev
)
492 int (*cb
)(struct device
*__dev
);
494 if (dev
->type
&& dev
->type
->pm
)
495 cb
= dev
->type
->pm
->runtime_suspend
;
496 else if (dev
->class && dev
->class->pm
)
497 cb
= dev
->class->pm
->runtime_suspend
;
498 else if (dev
->bus
&& dev
->bus
->pm
)
499 cb
= dev
->bus
->pm
->runtime_suspend
;
503 if (!cb
&& dev
->driver
&& dev
->driver
->pm
)
504 cb
= dev
->driver
->pm
->runtime_suspend
;
506 return cb
? cb(dev
) : 0;
510 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
511 * @dev: Device to handle.
513 static int __genpd_runtime_resume(struct device
*dev
)
515 int (*cb
)(struct device
*__dev
);
517 if (dev
->type
&& dev
->type
->pm
)
518 cb
= dev
->type
->pm
->runtime_resume
;
519 else if (dev
->class && dev
->class->pm
)
520 cb
= dev
->class->pm
->runtime_resume
;
521 else if (dev
->bus
&& dev
->bus
->pm
)
522 cb
= dev
->bus
->pm
->runtime_resume
;
526 if (!cb
&& dev
->driver
&& dev
->driver
->pm
)
527 cb
= dev
->driver
->pm
->runtime_resume
;
529 return cb
? cb(dev
) : 0;
533 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
534 * @dev: Device to suspend.
536 * Carry out a runtime suspend of a device under the assumption that its
537 * pm_domain field points to the domain member of an object of type
538 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
540 static int genpd_runtime_suspend(struct device
*dev
)
542 struct generic_pm_domain
*genpd
;
543 bool (*suspend_ok
)(struct device
*__dev
);
544 struct gpd_timing_data
*td
= &dev_gpd_data(dev
)->td
;
545 bool runtime_pm
= pm_runtime_enabled(dev
);
550 dev_dbg(dev
, "%s()\n", __func__
);
552 genpd
= dev_to_genpd(dev
);
557 * A runtime PM centric subsystem/driver may re-use the runtime PM
558 * callbacks for other purposes than runtime PM. In those scenarios
559 * runtime PM is disabled. Under these circumstances, we shall skip
560 * validating/measuring the PM QoS latency.
562 suspend_ok
= genpd
->gov
? genpd
->gov
->suspend_ok
: NULL
;
563 if (runtime_pm
&& suspend_ok
&& !suspend_ok(dev
))
566 /* Measure suspend latency. */
569 time_start
= ktime_get();
571 ret
= __genpd_runtime_suspend(dev
);
575 ret
= genpd_stop_dev(genpd
, dev
);
577 __genpd_runtime_resume(dev
);
581 /* Update suspend latency value if the measured time exceeds it. */
583 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
584 if (elapsed_ns
> td
->suspend_latency_ns
) {
585 td
->suspend_latency_ns
= elapsed_ns
;
586 dev_dbg(dev
, "suspend latency exceeded, %lld ns\n",
588 genpd
->max_off_time_changed
= true;
589 td
->constraint_changed
= true;
594 * If power.irq_safe is set, this routine may be run with
595 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
597 if (irq_safe_dev_in_no_sleep_domain(dev
, genpd
))
601 genpd_power_off(genpd
, true, 0);
608 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
609 * @dev: Device to resume.
611 * Carry out a runtime resume of a device under the assumption that its
612 * pm_domain field points to the domain member of an object of type
613 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
615 static int genpd_runtime_resume(struct device
*dev
)
617 struct generic_pm_domain
*genpd
;
618 struct gpd_timing_data
*td
= &dev_gpd_data(dev
)->td
;
619 bool runtime_pm
= pm_runtime_enabled(dev
);
625 dev_dbg(dev
, "%s()\n", __func__
);
627 genpd
= dev_to_genpd(dev
);
632 * As we don't power off a non IRQ safe domain, which holds
633 * an IRQ safe device, we don't need to restore power to it.
635 if (irq_safe_dev_in_no_sleep_domain(dev
, genpd
)) {
641 ret
= genpd_power_on(genpd
, 0);
648 /* Measure resume latency. */
650 if (timed
&& runtime_pm
)
651 time_start
= ktime_get();
653 ret
= genpd_start_dev(genpd
, dev
);
657 ret
= __genpd_runtime_resume(dev
);
661 /* Update resume latency value if the measured time exceeds it. */
662 if (timed
&& runtime_pm
) {
663 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
664 if (elapsed_ns
> td
->resume_latency_ns
) {
665 td
->resume_latency_ns
= elapsed_ns
;
666 dev_dbg(dev
, "resume latency exceeded, %lld ns\n",
668 genpd
->max_off_time_changed
= true;
669 td
->constraint_changed
= true;
676 genpd_stop_dev(genpd
, dev
);
678 if (!pm_runtime_is_irq_safe(dev
) ||
679 (pm_runtime_is_irq_safe(dev
) && genpd_is_irq_safe(genpd
))) {
681 genpd_power_off(genpd
, true, 0);
688 static bool pd_ignore_unused
;
689 static int __init
pd_ignore_unused_setup(char *__unused
)
691 pd_ignore_unused
= true;
694 __setup("pd_ignore_unused", pd_ignore_unused_setup
);
697 * genpd_power_off_unused - Power off all PM domains with no devices in use.
699 static int __init
genpd_power_off_unused(void)
701 struct generic_pm_domain
*genpd
;
703 if (pd_ignore_unused
) {
704 pr_warn("genpd: Not disabling unused power domains\n");
708 mutex_lock(&gpd_list_lock
);
710 list_for_each_entry(genpd
, &gpd_list
, gpd_list_node
)
711 genpd_queue_power_off_work(genpd
);
713 mutex_unlock(&gpd_list_lock
);
717 late_initcall(genpd_power_off_unused
);
719 #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
722 * pm_genpd_present - Check if the given PM domain has been initialized.
723 * @genpd: PM domain to check.
725 static bool pm_genpd_present(const struct generic_pm_domain
*genpd
)
727 const struct generic_pm_domain
*gpd
;
729 if (IS_ERR_OR_NULL(genpd
))
732 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
)
741 #ifdef CONFIG_PM_SLEEP
743 static bool genpd_dev_active_wakeup(const struct generic_pm_domain
*genpd
,
746 return GENPD_DEV_CALLBACK(genpd
, bool, active_wakeup
, dev
);
750 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
751 * @genpd: PM domain to power off, if possible.
752 * @use_lock: use the lock.
753 * @depth: nesting count for lockdep.
755 * Check if the given PM domain can be powered off (during system suspend or
756 * hibernation) and do that if so. Also, in that case propagate to its masters.
758 * This function is only called in "noirq" and "syscore" stages of system power
759 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
760 * these cases the lock must be held.
762 static void genpd_sync_power_off(struct generic_pm_domain
*genpd
, bool use_lock
,
765 struct gpd_link
*link
;
767 if (!genpd_status_on(genpd
) || genpd_is_always_on(genpd
))
770 if (genpd
->suspended_count
!= genpd
->device_count
771 || atomic_read(&genpd
->sd_count
) > 0)
774 /* Choose the deepest state when suspending */
775 genpd
->state_idx
= genpd
->state_count
- 1;
776 if (_genpd_power_off(genpd
, false))
779 genpd
->status
= GPD_STATE_POWER_OFF
;
781 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
782 genpd_sd_counter_dec(link
->master
);
785 genpd_lock_nested(link
->master
, depth
+ 1);
787 genpd_sync_power_off(link
->master
, use_lock
, depth
+ 1);
790 genpd_unlock(link
->master
);
795 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
796 * @genpd: PM domain to power on.
797 * @use_lock: use the lock.
798 * @depth: nesting count for lockdep.
800 * This function is only called in "noirq" and "syscore" stages of system power
801 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
802 * these cases the lock must be held.
804 static void genpd_sync_power_on(struct generic_pm_domain
*genpd
, bool use_lock
,
807 struct gpd_link
*link
;
809 if (genpd_status_on(genpd
))
812 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
813 genpd_sd_counter_inc(link
->master
);
816 genpd_lock_nested(link
->master
, depth
+ 1);
818 genpd_sync_power_on(link
->master
, use_lock
, depth
+ 1);
821 genpd_unlock(link
->master
);
824 _genpd_power_on(genpd
, false);
826 genpd
->status
= GPD_STATE_ACTIVE
;
830 * resume_needed - Check whether to resume a device before system suspend.
831 * @dev: Device to check.
832 * @genpd: PM domain the device belongs to.
834 * There are two cases in which a device that can wake up the system from sleep
835 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
836 * to wake up the system and it has to remain active for this purpose while the
837 * system is in the sleep state and (2) if the device is not enabled to wake up
838 * the system from sleep states and it generally doesn't generate wakeup signals
839 * by itself (those signals are generated on its behalf by other parts of the
840 * system). In the latter case it may be necessary to reconfigure the device's
841 * wakeup settings during system suspend, because it may have been set up to
842 * signal remote wakeup from the system's working state as needed by runtime PM.
843 * Return 'true' in either of the above cases.
845 static bool resume_needed(struct device
*dev
,
846 const struct generic_pm_domain
*genpd
)
850 if (!device_can_wakeup(dev
))
853 active_wakeup
= genpd_dev_active_wakeup(genpd
, dev
);
854 return device_may_wakeup(dev
) ? active_wakeup
: !active_wakeup
;
858 * pm_genpd_prepare - Start power transition of a device in a PM domain.
859 * @dev: Device to start the transition of.
861 * Start a power transition of a device (during a system-wide power transition)
862 * under the assumption that its pm_domain field points to the domain member of
863 * an object of type struct generic_pm_domain representing a PM domain
864 * consisting of I/O devices.
866 static int pm_genpd_prepare(struct device
*dev
)
868 struct generic_pm_domain
*genpd
;
871 dev_dbg(dev
, "%s()\n", __func__
);
873 genpd
= dev_to_genpd(dev
);
878 * If a wakeup request is pending for the device, it should be woken up
879 * at this point and a system wakeup event should be reported if it's
880 * set up to wake up the system from sleep states.
882 if (resume_needed(dev
, genpd
))
883 pm_runtime_resume(dev
);
887 if (genpd
->prepared_count
++ == 0)
888 genpd
->suspended_count
= 0;
892 ret
= pm_generic_prepare(dev
);
896 genpd
->prepared_count
--;
905 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
907 * @dev: Device to suspend.
908 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
910 * Stop the device and remove power from the domain if all devices in it have
913 static int genpd_finish_suspend(struct device
*dev
, bool poweroff
)
915 struct generic_pm_domain
*genpd
;
918 genpd
= dev_to_genpd(dev
);
922 if (dev
->power
.wakeup_path
&& genpd_dev_active_wakeup(genpd
, dev
))
926 ret
= pm_generic_poweroff_noirq(dev
);
928 ret
= pm_generic_suspend_noirq(dev
);
932 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
) {
933 ret
= pm_runtime_force_suspend(dev
);
939 genpd
->suspended_count
++;
940 genpd_sync_power_off(genpd
, true, 0);
947 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
948 * @dev: Device to suspend.
950 * Stop the device and remove power from the domain if all devices in it have
953 static int pm_genpd_suspend_noirq(struct device
*dev
)
955 dev_dbg(dev
, "%s()\n", __func__
);
957 return genpd_finish_suspend(dev
, false);
961 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
962 * @dev: Device to resume.
964 * Restore power to the device's PM domain, if necessary, and start the device.
966 static int pm_genpd_resume_noirq(struct device
*dev
)
968 struct generic_pm_domain
*genpd
;
971 dev_dbg(dev
, "%s()\n", __func__
);
973 genpd
= dev_to_genpd(dev
);
977 if (dev
->power
.wakeup_path
&& genpd_dev_active_wakeup(genpd
, dev
))
981 genpd_sync_power_on(genpd
, true, 0);
982 genpd
->suspended_count
--;
985 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
)
986 ret
= pm_runtime_force_resume(dev
);
988 ret
= pm_generic_resume_noirq(dev
);
996 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
997 * @dev: Device to freeze.
999 * Carry out a late freeze of a device under the assumption that its
1000 * pm_domain field points to the domain member of an object of type
1001 * struct generic_pm_domain representing a power domain consisting of I/O
1004 static int pm_genpd_freeze_noirq(struct device
*dev
)
1006 const struct generic_pm_domain
*genpd
;
1009 dev_dbg(dev
, "%s()\n", __func__
);
1011 genpd
= dev_to_genpd(dev
);
1015 ret
= pm_generic_freeze_noirq(dev
);
1019 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
)
1020 ret
= pm_runtime_force_suspend(dev
);
1026 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1027 * @dev: Device to thaw.
1029 * Start the device, unless power has been removed from the domain already
1030 * before the system transition.
1032 static int pm_genpd_thaw_noirq(struct device
*dev
)
1034 const struct generic_pm_domain
*genpd
;
1037 dev_dbg(dev
, "%s()\n", __func__
);
1039 genpd
= dev_to_genpd(dev
);
1043 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
) {
1044 ret
= pm_runtime_force_resume(dev
);
1049 return pm_generic_thaw_noirq(dev
);
1053 * pm_genpd_poweroff_noirq - Completion of hibernation of device in an
1055 * @dev: Device to poweroff.
1057 * Stop the device and remove power from the domain if all devices in it have
1060 static int pm_genpd_poweroff_noirq(struct device
*dev
)
1062 dev_dbg(dev
, "%s()\n", __func__
);
1064 return genpd_finish_suspend(dev
, true);
1068 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1069 * @dev: Device to resume.
1071 * Make sure the domain will be in the same power state as before the
1072 * hibernation the system is resuming from and start the device if necessary.
1074 static int pm_genpd_restore_noirq(struct device
*dev
)
1076 struct generic_pm_domain
*genpd
;
1079 dev_dbg(dev
, "%s()\n", __func__
);
1081 genpd
= dev_to_genpd(dev
);
1086 * At this point suspended_count == 0 means we are being run for the
1087 * first time for the given domain in the present cycle.
1090 if (genpd
->suspended_count
++ == 0)
1092 * The boot kernel might put the domain into arbitrary state,
1093 * so make it appear as powered off to genpd_sync_power_on(),
1094 * so that it tries to power it on in case it was really off.
1096 genpd
->status
= GPD_STATE_POWER_OFF
;
1098 genpd_sync_power_on(genpd
, true, 0);
1099 genpd_unlock(genpd
);
1101 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
) {
1102 ret
= pm_runtime_force_resume(dev
);
1107 return pm_generic_restore_noirq(dev
);
1111 * pm_genpd_complete - Complete power transition of a device in a power domain.
1112 * @dev: Device to complete the transition of.
1114 * Complete a power transition of a device (during a system-wide power
1115 * transition) under the assumption that its pm_domain field points to the
1116 * domain member of an object of type struct generic_pm_domain representing
1117 * a power domain consisting of I/O devices.
1119 static void pm_genpd_complete(struct device
*dev
)
1121 struct generic_pm_domain
*genpd
;
1123 dev_dbg(dev
, "%s()\n", __func__
);
1125 genpd
= dev_to_genpd(dev
);
1129 pm_generic_complete(dev
);
1133 genpd
->prepared_count
--;
1134 if (!genpd
->prepared_count
)
1135 genpd_queue_power_off_work(genpd
);
1137 genpd_unlock(genpd
);
1141 * genpd_syscore_switch - Switch power during system core suspend or resume.
1142 * @dev: Device that normally is marked as "always on" to switch power for.
1144 * This routine may only be called during the system core (syscore) suspend or
1145 * resume phase for devices whose "always on" flags are set.
1147 static void genpd_syscore_switch(struct device
*dev
, bool suspend
)
1149 struct generic_pm_domain
*genpd
;
1151 genpd
= genpd_lookup_dev(dev
);
1156 genpd
->suspended_count
++;
1157 genpd_sync_power_off(genpd
, false, 0);
1159 genpd_sync_power_on(genpd
, false, 0);
1160 genpd
->suspended_count
--;
1164 void pm_genpd_syscore_poweroff(struct device
*dev
)
1166 genpd_syscore_switch(dev
, true);
1168 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff
);
1170 void pm_genpd_syscore_poweron(struct device
*dev
)
1172 genpd_syscore_switch(dev
, false);
1174 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron
);
1176 #else /* !CONFIG_PM_SLEEP */
1178 #define pm_genpd_prepare NULL
1179 #define pm_genpd_suspend_noirq NULL
1180 #define pm_genpd_resume_noirq NULL
1181 #define pm_genpd_freeze_noirq NULL
1182 #define pm_genpd_thaw_noirq NULL
1183 #define pm_genpd_restore_noirq NULL
1184 #define pm_genpd_complete NULL
1186 #endif /* CONFIG_PM_SLEEP */
1188 static struct generic_pm_domain_data
*genpd_alloc_dev_data(struct device
*dev
,
1189 struct generic_pm_domain
*genpd
,
1190 struct gpd_timing_data
*td
)
1192 struct generic_pm_domain_data
*gpd_data
;
1195 ret
= dev_pm_get_subsys_data(dev
);
1197 return ERR_PTR(ret
);
1199 gpd_data
= kzalloc(sizeof(*gpd_data
), GFP_KERNEL
);
1208 gpd_data
->base
.dev
= dev
;
1209 gpd_data
->td
.constraint_changed
= true;
1210 gpd_data
->td
.effective_constraint_ns
= -1;
1211 gpd_data
->nb
.notifier_call
= genpd_dev_pm_qos_notifier
;
1213 spin_lock_irq(&dev
->power
.lock
);
1215 if (dev
->power
.subsys_data
->domain_data
) {
1220 dev
->power
.subsys_data
->domain_data
= &gpd_data
->base
;
1222 spin_unlock_irq(&dev
->power
.lock
);
1224 dev_pm_domain_set(dev
, &genpd
->domain
);
1229 spin_unlock_irq(&dev
->power
.lock
);
1232 dev_pm_put_subsys_data(dev
);
1233 return ERR_PTR(ret
);
1236 static void genpd_free_dev_data(struct device
*dev
,
1237 struct generic_pm_domain_data
*gpd_data
)
1239 dev_pm_domain_set(dev
, NULL
);
1241 spin_lock_irq(&dev
->power
.lock
);
1243 dev
->power
.subsys_data
->domain_data
= NULL
;
1245 spin_unlock_irq(&dev
->power
.lock
);
1248 dev_pm_put_subsys_data(dev
);
1251 static int genpd_add_device(struct generic_pm_domain
*genpd
, struct device
*dev
,
1252 struct gpd_timing_data
*td
)
1254 struct generic_pm_domain_data
*gpd_data
;
1257 dev_dbg(dev
, "%s()\n", __func__
);
1259 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(dev
))
1262 gpd_data
= genpd_alloc_dev_data(dev
, genpd
, td
);
1263 if (IS_ERR(gpd_data
))
1264 return PTR_ERR(gpd_data
);
1268 if (genpd
->prepared_count
> 0) {
1273 ret
= genpd
->attach_dev
? genpd
->attach_dev(genpd
, dev
) : 0;
1277 genpd
->device_count
++;
1278 genpd
->max_off_time_changed
= true;
1280 list_add_tail(&gpd_data
->base
.list_node
, &genpd
->dev_list
);
1283 genpd_unlock(genpd
);
1286 genpd_free_dev_data(dev
, gpd_data
);
1288 dev_pm_qos_add_notifier(dev
, &gpd_data
->nb
);
1294 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1295 * @genpd: PM domain to add the device to.
1296 * @dev: Device to be added.
1297 * @td: Set of PM QoS timing parameters to attach to the device.
1299 int __pm_genpd_add_device(struct generic_pm_domain
*genpd
, struct device
*dev
,
1300 struct gpd_timing_data
*td
)
1304 mutex_lock(&gpd_list_lock
);
1305 ret
= genpd_add_device(genpd
, dev
, td
);
1306 mutex_unlock(&gpd_list_lock
);
1310 EXPORT_SYMBOL_GPL(__pm_genpd_add_device
);
1312 static int genpd_remove_device(struct generic_pm_domain
*genpd
,
1315 struct generic_pm_domain_data
*gpd_data
;
1316 struct pm_domain_data
*pdd
;
1319 dev_dbg(dev
, "%s()\n", __func__
);
1321 pdd
= dev
->power
.subsys_data
->domain_data
;
1322 gpd_data
= to_gpd_data(pdd
);
1323 dev_pm_qos_remove_notifier(dev
, &gpd_data
->nb
);
1327 if (genpd
->prepared_count
> 0) {
1332 genpd
->device_count
--;
1333 genpd
->max_off_time_changed
= true;
1335 if (genpd
->detach_dev
)
1336 genpd
->detach_dev(genpd
, dev
);
1338 list_del_init(&pdd
->list_node
);
1340 genpd_unlock(genpd
);
1342 genpd_free_dev_data(dev
, gpd_data
);
1347 genpd_unlock(genpd
);
1348 dev_pm_qos_add_notifier(dev
, &gpd_data
->nb
);
1354 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1355 * @genpd: PM domain to remove the device from.
1356 * @dev: Device to be removed.
1358 int pm_genpd_remove_device(struct generic_pm_domain
*genpd
,
1361 if (!genpd
|| genpd
!= genpd_lookup_dev(dev
))
1364 return genpd_remove_device(genpd
, dev
);
1366 EXPORT_SYMBOL_GPL(pm_genpd_remove_device
);
1368 static int genpd_add_subdomain(struct generic_pm_domain
*genpd
,
1369 struct generic_pm_domain
*subdomain
)
1371 struct gpd_link
*link
, *itr
;
1374 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(subdomain
)
1375 || genpd
== subdomain
)
1379 * If the domain can be powered on/off in an IRQ safe
1380 * context, ensure that the subdomain can also be
1381 * powered on/off in that context.
1383 if (!genpd_is_irq_safe(genpd
) && genpd_is_irq_safe(subdomain
)) {
1384 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1385 genpd
->name
, subdomain
->name
);
1389 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
1393 genpd_lock(subdomain
);
1394 genpd_lock_nested(genpd
, SINGLE_DEPTH_NESTING
);
1396 if (!genpd_status_on(genpd
) && genpd_status_on(subdomain
)) {
1401 list_for_each_entry(itr
, &genpd
->master_links
, master_node
) {
1402 if (itr
->slave
== subdomain
&& itr
->master
== genpd
) {
1408 link
->master
= genpd
;
1409 list_add_tail(&link
->master_node
, &genpd
->master_links
);
1410 link
->slave
= subdomain
;
1411 list_add_tail(&link
->slave_node
, &subdomain
->slave_links
);
1412 if (genpd_status_on(subdomain
))
1413 genpd_sd_counter_inc(genpd
);
1416 genpd_unlock(genpd
);
1417 genpd_unlock(subdomain
);
1424 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1425 * @genpd: Master PM domain to add the subdomain to.
1426 * @subdomain: Subdomain to be added.
1428 int pm_genpd_add_subdomain(struct generic_pm_domain
*genpd
,
1429 struct generic_pm_domain
*subdomain
)
1433 mutex_lock(&gpd_list_lock
);
1434 ret
= genpd_add_subdomain(genpd
, subdomain
);
1435 mutex_unlock(&gpd_list_lock
);
1439 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain
);
1442 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1443 * @genpd: Master PM domain to remove the subdomain from.
1444 * @subdomain: Subdomain to be removed.
1446 int pm_genpd_remove_subdomain(struct generic_pm_domain
*genpd
,
1447 struct generic_pm_domain
*subdomain
)
1449 struct gpd_link
*l
, *link
;
1452 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(subdomain
))
1455 genpd_lock(subdomain
);
1456 genpd_lock_nested(genpd
, SINGLE_DEPTH_NESTING
);
1458 if (!list_empty(&subdomain
->master_links
) || subdomain
->device_count
) {
1459 pr_warn("%s: unable to remove subdomain %s\n", genpd
->name
,
1465 list_for_each_entry_safe(link
, l
, &genpd
->master_links
, master_node
) {
1466 if (link
->slave
!= subdomain
)
1469 list_del(&link
->master_node
);
1470 list_del(&link
->slave_node
);
1472 if (genpd_status_on(subdomain
))
1473 genpd_sd_counter_dec(genpd
);
1480 genpd_unlock(genpd
);
1481 genpd_unlock(subdomain
);
1485 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain
);
1487 static int genpd_set_default_power_state(struct generic_pm_domain
*genpd
)
1489 struct genpd_power_state
*state
;
1491 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1495 genpd
->states
= state
;
1496 genpd
->state_count
= 1;
1497 genpd
->free
= state
;
1502 static void genpd_lock_init(struct generic_pm_domain
*genpd
)
1504 if (genpd
->flags
& GENPD_FLAG_IRQ_SAFE
) {
1505 spin_lock_init(&genpd
->slock
);
1506 genpd
->lock_ops
= &genpd_spin_ops
;
1508 mutex_init(&genpd
->mlock
);
1509 genpd
->lock_ops
= &genpd_mtx_ops
;
1514 * pm_genpd_init - Initialize a generic I/O PM domain object.
1515 * @genpd: PM domain object to initialize.
1516 * @gov: PM domain governor to associate with the domain (may be NULL).
1517 * @is_off: Initial value of the domain's power_is_off field.
1519 * Returns 0 on successful initialization, else a negative error code.
1521 int pm_genpd_init(struct generic_pm_domain
*genpd
,
1522 struct dev_power_governor
*gov
, bool is_off
)
1526 if (IS_ERR_OR_NULL(genpd
))
1529 INIT_LIST_HEAD(&genpd
->master_links
);
1530 INIT_LIST_HEAD(&genpd
->slave_links
);
1531 INIT_LIST_HEAD(&genpd
->dev_list
);
1532 genpd_lock_init(genpd
);
1534 INIT_WORK(&genpd
->power_off_work
, genpd_power_off_work_fn
);
1535 atomic_set(&genpd
->sd_count
, 0);
1536 genpd
->status
= is_off
? GPD_STATE_POWER_OFF
: GPD_STATE_ACTIVE
;
1537 genpd
->device_count
= 0;
1538 genpd
->max_off_time_ns
= -1;
1539 genpd
->max_off_time_changed
= true;
1540 genpd
->provider
= NULL
;
1541 genpd
->has_provider
= false;
1542 genpd
->domain
.ops
.runtime_suspend
= genpd_runtime_suspend
;
1543 genpd
->domain
.ops
.runtime_resume
= genpd_runtime_resume
;
1544 genpd
->domain
.ops
.prepare
= pm_genpd_prepare
;
1545 genpd
->domain
.ops
.suspend_noirq
= pm_genpd_suspend_noirq
;
1546 genpd
->domain
.ops
.resume_noirq
= pm_genpd_resume_noirq
;
1547 genpd
->domain
.ops
.freeze_noirq
= pm_genpd_freeze_noirq
;
1548 genpd
->domain
.ops
.thaw_noirq
= pm_genpd_thaw_noirq
;
1549 genpd
->domain
.ops
.poweroff_noirq
= pm_genpd_poweroff_noirq
;
1550 genpd
->domain
.ops
.restore_noirq
= pm_genpd_restore_noirq
;
1551 genpd
->domain
.ops
.complete
= pm_genpd_complete
;
1553 if (genpd
->flags
& GENPD_FLAG_PM_CLK
) {
1554 genpd
->dev_ops
.stop
= pm_clk_suspend
;
1555 genpd
->dev_ops
.start
= pm_clk_resume
;
1558 /* Always-on domains must be powered on at initialization. */
1559 if (genpd_is_always_on(genpd
) && !genpd_status_on(genpd
))
1562 /* Use only one "off" state if there were no states declared */
1563 if (genpd
->state_count
== 0) {
1564 ret
= genpd_set_default_power_state(genpd
);
1569 mutex_lock(&gpd_list_lock
);
1570 list_add(&genpd
->gpd_list_node
, &gpd_list
);
1571 mutex_unlock(&gpd_list_lock
);
1575 EXPORT_SYMBOL_GPL(pm_genpd_init
);
1577 static int genpd_remove(struct generic_pm_domain
*genpd
)
1579 struct gpd_link
*l
, *link
;
1581 if (IS_ERR_OR_NULL(genpd
))
1586 if (genpd
->has_provider
) {
1587 genpd_unlock(genpd
);
1588 pr_err("Provider present, unable to remove %s\n", genpd
->name
);
1592 if (!list_empty(&genpd
->master_links
) || genpd
->device_count
) {
1593 genpd_unlock(genpd
);
1594 pr_err("%s: unable to remove %s\n", __func__
, genpd
->name
);
1598 list_for_each_entry_safe(link
, l
, &genpd
->slave_links
, slave_node
) {
1599 list_del(&link
->master_node
);
1600 list_del(&link
->slave_node
);
1604 list_del(&genpd
->gpd_list_node
);
1605 genpd_unlock(genpd
);
1606 cancel_work_sync(&genpd
->power_off_work
);
1608 pr_debug("%s: removed %s\n", __func__
, genpd
->name
);
1614 * pm_genpd_remove - Remove a generic I/O PM domain
1615 * @genpd: Pointer to PM domain that is to be removed.
1617 * To remove the PM domain, this function:
1618 * - Removes the PM domain as a subdomain to any parent domains,
1620 * - Removes the PM domain from the list of registered PM domains.
1622 * The PM domain will only be removed, if the associated provider has
1623 * been removed, it is not a parent to any other PM domain and has no
1624 * devices associated with it.
1626 int pm_genpd_remove(struct generic_pm_domain
*genpd
)
1630 mutex_lock(&gpd_list_lock
);
1631 ret
= genpd_remove(genpd
);
1632 mutex_unlock(&gpd_list_lock
);
1636 EXPORT_SYMBOL_GPL(pm_genpd_remove
);
1638 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1640 typedef struct generic_pm_domain
*(*genpd_xlate_t
)(struct of_phandle_args
*args
,
1644 * Device Tree based PM domain providers.
1646 * The code below implements generic device tree based PM domain providers that
1647 * bind device tree nodes with generic PM domains registered in the system.
1649 * Any driver that registers generic PM domains and needs to support binding of
1650 * devices to these domains is supposed to register a PM domain provider, which
1651 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1653 * Two simple mapping functions have been provided for convenience:
1654 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1655 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1660 * struct of_genpd_provider - PM domain provider registration structure
1661 * @link: Entry in global list of PM domain providers
1662 * @node: Pointer to device tree node of PM domain provider
1663 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1665 * @data: context pointer to be passed into @xlate callback
1667 struct of_genpd_provider
{
1668 struct list_head link
;
1669 struct device_node
*node
;
1670 genpd_xlate_t xlate
;
1674 /* List of registered PM domain providers. */
1675 static LIST_HEAD(of_genpd_providers
);
1676 /* Mutex to protect the list above. */
1677 static DEFINE_MUTEX(of_genpd_mutex
);
1680 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1681 * @genpdspec: OF phandle args to map into a PM domain
1682 * @data: xlate function private data - pointer to struct generic_pm_domain
1684 * This is a generic xlate function that can be used to model PM domains that
1685 * have their own device tree nodes. The private data of xlate function needs
1686 * to be a valid pointer to struct generic_pm_domain.
1688 static struct generic_pm_domain
*genpd_xlate_simple(
1689 struct of_phandle_args
*genpdspec
,
1696 * genpd_xlate_onecell() - Xlate function using a single index.
1697 * @genpdspec: OF phandle args to map into a PM domain
1698 * @data: xlate function private data - pointer to struct genpd_onecell_data
1700 * This is a generic xlate function that can be used to model simple PM domain
1701 * controllers that have one device tree node and provide multiple PM domains.
1702 * A single cell is used as an index into an array of PM domains specified in
1703 * the genpd_onecell_data struct when registering the provider.
1705 static struct generic_pm_domain
*genpd_xlate_onecell(
1706 struct of_phandle_args
*genpdspec
,
1709 struct genpd_onecell_data
*genpd_data
= data
;
1710 unsigned int idx
= genpdspec
->args
[0];
1712 if (genpdspec
->args_count
!= 1)
1713 return ERR_PTR(-EINVAL
);
1715 if (idx
>= genpd_data
->num_domains
) {
1716 pr_err("%s: invalid domain index %u\n", __func__
, idx
);
1717 return ERR_PTR(-EINVAL
);
1720 if (!genpd_data
->domains
[idx
])
1721 return ERR_PTR(-ENOENT
);
1723 return genpd_data
->domains
[idx
];
1727 * genpd_add_provider() - Register a PM domain provider for a node
1728 * @np: Device node pointer associated with the PM domain provider.
1729 * @xlate: Callback for decoding PM domain from phandle arguments.
1730 * @data: Context pointer for @xlate callback.
1732 static int genpd_add_provider(struct device_node
*np
, genpd_xlate_t xlate
,
1735 struct of_genpd_provider
*cp
;
1737 cp
= kzalloc(sizeof(*cp
), GFP_KERNEL
);
1741 cp
->node
= of_node_get(np
);
1745 mutex_lock(&of_genpd_mutex
);
1746 list_add(&cp
->link
, &of_genpd_providers
);
1747 mutex_unlock(&of_genpd_mutex
);
1748 pr_debug("Added domain provider from %s\n", np
->full_name
);
1754 * of_genpd_add_provider_simple() - Register a simple PM domain provider
1755 * @np: Device node pointer associated with the PM domain provider.
1756 * @genpd: Pointer to PM domain associated with the PM domain provider.
1758 int of_genpd_add_provider_simple(struct device_node
*np
,
1759 struct generic_pm_domain
*genpd
)
1766 mutex_lock(&gpd_list_lock
);
1768 if (pm_genpd_present(genpd
)) {
1769 ret
= genpd_add_provider(np
, genpd_xlate_simple
, genpd
);
1771 genpd
->provider
= &np
->fwnode
;
1772 genpd
->has_provider
= true;
1776 mutex_unlock(&gpd_list_lock
);
1780 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple
);
1783 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
1784 * @np: Device node pointer associated with the PM domain provider.
1785 * @data: Pointer to the data associated with the PM domain provider.
1787 int of_genpd_add_provider_onecell(struct device_node
*np
,
1788 struct genpd_onecell_data
*data
)
1796 mutex_lock(&gpd_list_lock
);
1798 for (i
= 0; i
< data
->num_domains
; i
++) {
1799 if (!data
->domains
[i
])
1801 if (!pm_genpd_present(data
->domains
[i
]))
1804 data
->domains
[i
]->provider
= &np
->fwnode
;
1805 data
->domains
[i
]->has_provider
= true;
1808 ret
= genpd_add_provider(np
, genpd_xlate_onecell
, data
);
1812 mutex_unlock(&gpd_list_lock
);
1818 if (!data
->domains
[i
])
1820 data
->domains
[i
]->provider
= NULL
;
1821 data
->domains
[i
]->has_provider
= false;
1824 mutex_unlock(&gpd_list_lock
);
1828 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell
);
1831 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1832 * @np: Device node pointer associated with the PM domain provider
1834 void of_genpd_del_provider(struct device_node
*np
)
1836 struct of_genpd_provider
*cp
, *tmp
;
1837 struct generic_pm_domain
*gpd
;
1839 mutex_lock(&gpd_list_lock
);
1840 mutex_lock(&of_genpd_mutex
);
1841 list_for_each_entry_safe(cp
, tmp
, &of_genpd_providers
, link
) {
1842 if (cp
->node
== np
) {
1844 * For each PM domain associated with the
1845 * provider, set the 'has_provider' to false
1846 * so that the PM domain can be safely removed.
1848 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
)
1849 if (gpd
->provider
== &np
->fwnode
)
1850 gpd
->has_provider
= false;
1852 list_del(&cp
->link
);
1853 of_node_put(cp
->node
);
1858 mutex_unlock(&of_genpd_mutex
);
1859 mutex_unlock(&gpd_list_lock
);
1861 EXPORT_SYMBOL_GPL(of_genpd_del_provider
);
1864 * genpd_get_from_provider() - Look-up PM domain
1865 * @genpdspec: OF phandle args to use for look-up
1867 * Looks for a PM domain provider under the node specified by @genpdspec and if
1868 * found, uses xlate function of the provider to map phandle args to a PM
1871 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1874 static struct generic_pm_domain
*genpd_get_from_provider(
1875 struct of_phandle_args
*genpdspec
)
1877 struct generic_pm_domain
*genpd
= ERR_PTR(-ENOENT
);
1878 struct of_genpd_provider
*provider
;
1881 return ERR_PTR(-EINVAL
);
1883 mutex_lock(&of_genpd_mutex
);
1885 /* Check if we have such a provider in our array */
1886 list_for_each_entry(provider
, &of_genpd_providers
, link
) {
1887 if (provider
->node
== genpdspec
->np
)
1888 genpd
= provider
->xlate(genpdspec
, provider
->data
);
1893 mutex_unlock(&of_genpd_mutex
);
1899 * of_genpd_add_device() - Add a device to an I/O PM domain
1900 * @genpdspec: OF phandle args to use for look-up PM domain
1901 * @dev: Device to be added.
1903 * Looks-up an I/O PM domain based upon phandle args provided and adds
1904 * the device to the PM domain. Returns a negative error code on failure.
1906 int of_genpd_add_device(struct of_phandle_args
*genpdspec
, struct device
*dev
)
1908 struct generic_pm_domain
*genpd
;
1911 mutex_lock(&gpd_list_lock
);
1913 genpd
= genpd_get_from_provider(genpdspec
);
1914 if (IS_ERR(genpd
)) {
1915 ret
= PTR_ERR(genpd
);
1919 ret
= genpd_add_device(genpd
, dev
, NULL
);
1922 mutex_unlock(&gpd_list_lock
);
1926 EXPORT_SYMBOL_GPL(of_genpd_add_device
);
1929 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1930 * @parent_spec: OF phandle args to use for parent PM domain look-up
1931 * @subdomain_spec: OF phandle args to use for subdomain look-up
1933 * Looks-up a parent PM domain and subdomain based upon phandle args
1934 * provided and adds the subdomain to the parent PM domain. Returns a
1935 * negative error code on failure.
1937 int of_genpd_add_subdomain(struct of_phandle_args
*parent_spec
,
1938 struct of_phandle_args
*subdomain_spec
)
1940 struct generic_pm_domain
*parent
, *subdomain
;
1943 mutex_lock(&gpd_list_lock
);
1945 parent
= genpd_get_from_provider(parent_spec
);
1946 if (IS_ERR(parent
)) {
1947 ret
= PTR_ERR(parent
);
1951 subdomain
= genpd_get_from_provider(subdomain_spec
);
1952 if (IS_ERR(subdomain
)) {
1953 ret
= PTR_ERR(subdomain
);
1957 ret
= genpd_add_subdomain(parent
, subdomain
);
1960 mutex_unlock(&gpd_list_lock
);
1964 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain
);
1967 * of_genpd_remove_last - Remove the last PM domain registered for a provider
1968 * @provider: Pointer to device structure associated with provider
1970 * Find the last PM domain that was added by a particular provider and
1971 * remove this PM domain from the list of PM domains. The provider is
1972 * identified by the 'provider' device structure that is passed. The PM
1973 * domain will only be removed, if the provider associated with domain
1976 * Returns a valid pointer to struct generic_pm_domain on success or
1977 * ERR_PTR() on failure.
1979 struct generic_pm_domain
*of_genpd_remove_last(struct device_node
*np
)
1981 struct generic_pm_domain
*gpd
, *tmp
, *genpd
= ERR_PTR(-ENOENT
);
1984 if (IS_ERR_OR_NULL(np
))
1985 return ERR_PTR(-EINVAL
);
1987 mutex_lock(&gpd_list_lock
);
1988 list_for_each_entry_safe(gpd
, tmp
, &gpd_list
, gpd_list_node
) {
1989 if (gpd
->provider
== &np
->fwnode
) {
1990 ret
= genpd_remove(gpd
);
1991 genpd
= ret
? ERR_PTR(ret
) : gpd
;
1995 mutex_unlock(&gpd_list_lock
);
1999 EXPORT_SYMBOL_GPL(of_genpd_remove_last
);
2002 * genpd_dev_pm_detach - Detach a device from its PM domain.
2003 * @dev: Device to detach.
2004 * @power_off: Currently not used
2006 * Try to locate a corresponding generic PM domain, which the device was
2007 * attached to previously. If such is found, the device is detached from it.
2009 static void genpd_dev_pm_detach(struct device
*dev
, bool power_off
)
2011 struct generic_pm_domain
*pd
;
2015 pd
= dev_to_genpd(dev
);
2019 dev_dbg(dev
, "removing from PM domain %s\n", pd
->name
);
2021 for (i
= 1; i
< GENPD_RETRY_MAX_MS
; i
<<= 1) {
2022 ret
= genpd_remove_device(pd
, dev
);
2031 dev_err(dev
, "failed to remove from PM domain %s: %d",
2036 /* Check if PM domain can be powered off after removing this device. */
2037 genpd_queue_power_off_work(pd
);
2040 static void genpd_dev_pm_sync(struct device
*dev
)
2042 struct generic_pm_domain
*pd
;
2044 pd
= dev_to_genpd(dev
);
2048 genpd_queue_power_off_work(pd
);
2052 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2053 * @dev: Device to attach.
2055 * Parse device's OF node to find a PM domain specifier. If such is found,
2056 * attaches the device to retrieved pm_domain ops.
2058 * Both generic and legacy Samsung-specific DT bindings are supported to keep
2059 * backwards compatibility with existing DTBs.
2061 * Returns 0 on successfully attached PM domain or negative error code. Note
2062 * that if a power-domain exists for the device, but it cannot be found or
2063 * turned on, then return -EPROBE_DEFER to ensure that the device is not
2064 * probed and to re-try again later.
2066 int genpd_dev_pm_attach(struct device
*dev
)
2068 struct of_phandle_args pd_args
;
2069 struct generic_pm_domain
*pd
;
2079 ret
= of_parse_phandle_with_args(dev
->of_node
, "power-domains",
2080 "#power-domain-cells", 0, &pd_args
);
2086 * Try legacy Samsung-specific bindings
2087 * (for backwards compatibility of DT ABI)
2089 pd_args
.args_count
= 0;
2090 pd_args
.np
= of_parse_phandle(dev
->of_node
,
2091 "samsung,power-domain", 0);
2096 mutex_lock(&gpd_list_lock
);
2097 pd
= genpd_get_from_provider(&pd_args
);
2098 of_node_put(pd_args
.np
);
2100 mutex_unlock(&gpd_list_lock
);
2101 dev_dbg(dev
, "%s() failed to find PM domain: %ld\n",
2102 __func__
, PTR_ERR(pd
));
2103 return -EPROBE_DEFER
;
2106 dev_dbg(dev
, "adding to PM domain %s\n", pd
->name
);
2108 for (i
= 1; i
< GENPD_RETRY_MAX_MS
; i
<<= 1) {
2109 ret
= genpd_add_device(pd
, dev
, NULL
);
2116 mutex_unlock(&gpd_list_lock
);
2119 if (ret
!= -EPROBE_DEFER
)
2120 dev_err(dev
, "failed to add to PM domain %s: %d",
2125 dev
->pm_domain
->detach
= genpd_dev_pm_detach
;
2126 dev
->pm_domain
->sync
= genpd_dev_pm_sync
;
2129 ret
= genpd_power_on(pd
, 0);
2132 return ret
? -EPROBE_DEFER
: 0;
2134 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach
);
2136 static const struct of_device_id idle_state_match
[] = {
2137 { .compatible
= "domain-idle-state", },
2141 static int genpd_parse_state(struct genpd_power_state
*genpd_state
,
2142 struct device_node
*state_node
)
2146 u32 entry_latency
, exit_latency
;
2148 err
= of_property_read_u32(state_node
, "entry-latency-us",
2151 pr_debug(" * %s missing entry-latency-us property\n",
2152 state_node
->full_name
);
2156 err
= of_property_read_u32(state_node
, "exit-latency-us",
2159 pr_debug(" * %s missing exit-latency-us property\n",
2160 state_node
->full_name
);
2164 err
= of_property_read_u32(state_node
, "min-residency-us", &residency
);
2166 genpd_state
->residency_ns
= 1000 * residency
;
2168 genpd_state
->power_on_latency_ns
= 1000 * exit_latency
;
2169 genpd_state
->power_off_latency_ns
= 1000 * entry_latency
;
2170 genpd_state
->fwnode
= &state_node
->fwnode
;
2176 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2178 * @dn: The genpd device node
2179 * @states: The pointer to which the state array will be saved.
2180 * @n: The count of elements in the array returned from this function.
2182 * Returns the device states parsed from the OF node. The memory for the states
2183 * is allocated by this function and is the responsibility of the caller to
2184 * free the memory after use.
2186 int of_genpd_parse_idle_states(struct device_node
*dn
,
2187 struct genpd_power_state
**states
, int *n
)
2189 struct genpd_power_state
*st
;
2190 struct device_node
*np
;
2194 struct of_phandle_iterator it
;
2195 const struct of_device_id
*match_id
;
2197 count
= of_count_phandle_with_args(dn
, "domain-idle-states", NULL
);
2201 st
= kcalloc(count
, sizeof(*st
), GFP_KERNEL
);
2205 /* Loop over the phandles until all the requested entry is found */
2206 of_for_each_phandle(&it
, err
, dn
, "domain-idle-states", NULL
, 0) {
2208 match_id
= of_match_node(idle_state_match
, np
);
2211 ret
= genpd_parse_state(&st
[i
++], np
);
2214 ("Parsing idle state node %s failed with err %d\n",
2215 np
->full_name
, ret
);
2230 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states
);
2232 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2235 /*** debugfs support ***/
2237 #ifdef CONFIG_DEBUG_FS
2238 #include <linux/pm.h>
2239 #include <linux/device.h>
2240 #include <linux/debugfs.h>
2241 #include <linux/seq_file.h>
2242 #include <linux/init.h>
2243 #include <linux/kobject.h>
2244 static struct dentry
*pm_genpd_debugfs_dir
;
2247 * TODO: This function is a slightly modified version of rtpm_status_show
2248 * from sysfs.c, so generalize it.
2250 static void rtpm_status_str(struct seq_file
*s
, struct device
*dev
)
2252 static const char * const status_lookup
[] = {
2253 [RPM_ACTIVE
] = "active",
2254 [RPM_RESUMING
] = "resuming",
2255 [RPM_SUSPENDED
] = "suspended",
2256 [RPM_SUSPENDING
] = "suspending"
2260 if (dev
->power
.runtime_error
)
2262 else if (dev
->power
.disable_depth
)
2264 else if (dev
->power
.runtime_status
< ARRAY_SIZE(status_lookup
))
2265 p
= status_lookup
[dev
->power
.runtime_status
];
2272 static int pm_genpd_summary_one(struct seq_file
*s
,
2273 struct generic_pm_domain
*genpd
)
2275 static const char * const status_lookup
[] = {
2276 [GPD_STATE_ACTIVE
] = "on",
2277 [GPD_STATE_POWER_OFF
] = "off"
2279 struct pm_domain_data
*pm_data
;
2280 const char *kobj_path
;
2281 struct gpd_link
*link
;
2285 ret
= genpd_lock_interruptible(genpd
);
2287 return -ERESTARTSYS
;
2289 if (WARN_ON(genpd
->status
>= ARRAY_SIZE(status_lookup
)))
2291 if (!genpd_status_on(genpd
))
2292 snprintf(state
, sizeof(state
), "%s-%u",
2293 status_lookup
[genpd
->status
], genpd
->state_idx
);
2295 snprintf(state
, sizeof(state
), "%s",
2296 status_lookup
[genpd
->status
]);
2297 seq_printf(s
, "%-30s %-15s ", genpd
->name
, state
);
2300 * Modifications on the list require holding locks on both
2301 * master and slave, so we are safe.
2302 * Also genpd->name is immutable.
2304 list_for_each_entry(link
, &genpd
->master_links
, master_node
) {
2305 seq_printf(s
, "%s", link
->slave
->name
);
2306 if (!list_is_last(&link
->master_node
, &genpd
->master_links
))
2310 list_for_each_entry(pm_data
, &genpd
->dev_list
, list_node
) {
2311 kobj_path
= kobject_get_path(&pm_data
->dev
->kobj
,
2312 genpd_is_irq_safe(genpd
) ?
2313 GFP_ATOMIC
: GFP_KERNEL
);
2314 if (kobj_path
== NULL
)
2317 seq_printf(s
, "\n %-50s ", kobj_path
);
2318 rtpm_status_str(s
, pm_data
->dev
);
2324 genpd_unlock(genpd
);
2329 static int pm_genpd_summary_show(struct seq_file
*s
, void *data
)
2331 struct generic_pm_domain
*genpd
;
2334 seq_puts(s
, "domain status slaves\n");
2335 seq_puts(s
, " /device runtime status\n");
2336 seq_puts(s
, "----------------------------------------------------------------------\n");
2338 ret
= mutex_lock_interruptible(&gpd_list_lock
);
2340 return -ERESTARTSYS
;
2342 list_for_each_entry(genpd
, &gpd_list
, gpd_list_node
) {
2343 ret
= pm_genpd_summary_one(s
, genpd
);
2347 mutex_unlock(&gpd_list_lock
);
2352 static int pm_genpd_summary_open(struct inode
*inode
, struct file
*file
)
2354 return single_open(file
, pm_genpd_summary_show
, NULL
);
2357 static const struct file_operations pm_genpd_summary_fops
= {
2358 .open
= pm_genpd_summary_open
,
2360 .llseek
= seq_lseek
,
2361 .release
= single_release
,
2364 static int __init
pm_genpd_debug_init(void)
2368 pm_genpd_debugfs_dir
= debugfs_create_dir("pm_genpd", NULL
);
2370 if (!pm_genpd_debugfs_dir
)
2373 d
= debugfs_create_file("pm_genpd_summary", S_IRUGO
,
2374 pm_genpd_debugfs_dir
, NULL
, &pm_genpd_summary_fops
);
2380 late_initcall(pm_genpd_debug_init
);
2382 static void __exit
pm_genpd_debug_exit(void)
2384 debugfs_remove_recursive(pm_genpd_debugfs_dir
);
2386 __exitcall(pm_genpd_debug_exit
);
2387 #endif /* CONFIG_DEBUG_FS */