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_opp.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/pm_domain.h>
16 #include <linux/pm_qos.h>
17 #include <linux/pm_clock.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/sched.h>
21 #include <linux/suspend.h>
22 #include <linux/export.h>
26 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
28 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
30 type (*__routine)(struct device *__d); \
31 type __ret = (type)0; \
33 __routine = genpd->dev_ops.callback; \
35 __ret = __routine(dev); \
40 static LIST_HEAD(gpd_list
);
41 static DEFINE_MUTEX(gpd_list_lock
);
43 struct genpd_lock_ops
{
44 void (*lock
)(struct generic_pm_domain
*genpd
);
45 void (*lock_nested
)(struct generic_pm_domain
*genpd
, int depth
);
46 int (*lock_interruptible
)(struct generic_pm_domain
*genpd
);
47 void (*unlock
)(struct generic_pm_domain
*genpd
);
50 static void genpd_lock_mtx(struct generic_pm_domain
*genpd
)
52 mutex_lock(&genpd
->mlock
);
55 static void genpd_lock_nested_mtx(struct generic_pm_domain
*genpd
,
58 mutex_lock_nested(&genpd
->mlock
, depth
);
61 static int genpd_lock_interruptible_mtx(struct generic_pm_domain
*genpd
)
63 return mutex_lock_interruptible(&genpd
->mlock
);
66 static void genpd_unlock_mtx(struct generic_pm_domain
*genpd
)
68 return mutex_unlock(&genpd
->mlock
);
71 static const struct genpd_lock_ops genpd_mtx_ops
= {
72 .lock
= genpd_lock_mtx
,
73 .lock_nested
= genpd_lock_nested_mtx
,
74 .lock_interruptible
= genpd_lock_interruptible_mtx
,
75 .unlock
= genpd_unlock_mtx
,
78 static void genpd_lock_spin(struct generic_pm_domain
*genpd
)
79 __acquires(&genpd
->slock
)
83 spin_lock_irqsave(&genpd
->slock
, flags
);
84 genpd
->lock_flags
= flags
;
87 static void genpd_lock_nested_spin(struct generic_pm_domain
*genpd
,
89 __acquires(&genpd
->slock
)
93 spin_lock_irqsave_nested(&genpd
->slock
, flags
, depth
);
94 genpd
->lock_flags
= flags
;
97 static int genpd_lock_interruptible_spin(struct generic_pm_domain
*genpd
)
98 __acquires(&genpd
->slock
)
102 spin_lock_irqsave(&genpd
->slock
, flags
);
103 genpd
->lock_flags
= flags
;
107 static void genpd_unlock_spin(struct generic_pm_domain
*genpd
)
108 __releases(&genpd
->slock
)
110 spin_unlock_irqrestore(&genpd
->slock
, genpd
->lock_flags
);
113 static const struct genpd_lock_ops genpd_spin_ops
= {
114 .lock
= genpd_lock_spin
,
115 .lock_nested
= genpd_lock_nested_spin
,
116 .lock_interruptible
= genpd_lock_interruptible_spin
,
117 .unlock
= genpd_unlock_spin
,
120 #define genpd_lock(p) p->lock_ops->lock(p)
121 #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
122 #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
123 #define genpd_unlock(p) p->lock_ops->unlock(p)
125 #define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
126 #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
127 #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
128 #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
130 static inline bool irq_safe_dev_in_no_sleep_domain(struct device
*dev
,
131 const struct generic_pm_domain
*genpd
)
135 ret
= pm_runtime_is_irq_safe(dev
) && !genpd_is_irq_safe(genpd
);
138 * Warn once if an IRQ safe device is attached to a no sleep domain, as
139 * to indicate a suboptimal configuration for PM. For an always on
140 * domain this isn't case, thus don't warn.
142 if (ret
&& !genpd_is_always_on(genpd
))
143 dev_warn_once(dev
, "PM domain %s will not be powered off\n",
150 * Get the generic PM domain for a particular struct device.
151 * This validates the struct device pointer, the PM domain pointer,
152 * and checks that the PM domain pointer is a real generic PM domain.
153 * Any failure results in NULL being returned.
155 static struct generic_pm_domain
*genpd_lookup_dev(struct device
*dev
)
157 struct generic_pm_domain
*genpd
= NULL
, *gpd
;
159 if (IS_ERR_OR_NULL(dev
) || IS_ERR_OR_NULL(dev
->pm_domain
))
162 mutex_lock(&gpd_list_lock
);
163 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
) {
164 if (&gpd
->domain
== dev
->pm_domain
) {
169 mutex_unlock(&gpd_list_lock
);
175 * This should only be used where we are certain that the pm_domain
176 * attached to the device is a genpd domain.
178 static struct generic_pm_domain
*dev_to_genpd(struct device
*dev
)
180 if (IS_ERR_OR_NULL(dev
->pm_domain
))
181 return ERR_PTR(-EINVAL
);
183 return pd_to_genpd(dev
->pm_domain
);
186 static int genpd_stop_dev(const struct generic_pm_domain
*genpd
,
189 return GENPD_DEV_CALLBACK(genpd
, int, stop
, dev
);
192 static int genpd_start_dev(const struct generic_pm_domain
*genpd
,
195 return GENPD_DEV_CALLBACK(genpd
, int, start
, dev
);
198 static bool genpd_sd_counter_dec(struct generic_pm_domain
*genpd
)
202 if (!WARN_ON(atomic_read(&genpd
->sd_count
) == 0))
203 ret
= !!atomic_dec_and_test(&genpd
->sd_count
);
208 static void genpd_sd_counter_inc(struct generic_pm_domain
*genpd
)
210 atomic_inc(&genpd
->sd_count
);
211 smp_mb__after_atomic();
214 #ifdef CONFIG_DEBUG_FS
215 static void genpd_update_accounting(struct generic_pm_domain
*genpd
)
220 delta
= ktime_sub(now
, genpd
->accounting_time
);
223 * If genpd->status is active, it means we are just
224 * out of off and so update the idle time and vice
227 if (genpd
->status
== GPD_STATE_ACTIVE
) {
228 int state_idx
= genpd
->state_idx
;
230 genpd
->states
[state_idx
].idle_time
=
231 ktime_add(genpd
->states
[state_idx
].idle_time
, delta
);
233 genpd
->on_time
= ktime_add(genpd
->on_time
, delta
);
236 genpd
->accounting_time
= now
;
239 static inline void genpd_update_accounting(struct generic_pm_domain
*genpd
) {}
243 * dev_pm_genpd_set_performance_state- Set performance state of device's power
246 * @dev: Device for which the performance-state needs to be set.
247 * @state: Target performance state of the device. This can be set as 0 when the
248 * device doesn't have any performance state constraints left (And so
249 * the device wouldn't participate anymore to find the target
250 * performance state of the genpd).
252 * It is assumed that the users guarantee that the genpd wouldn't be detached
253 * while this routine is getting called.
255 * Returns 0 on success and negative error values on failures.
257 int dev_pm_genpd_set_performance_state(struct device
*dev
, unsigned int state
)
259 struct generic_pm_domain
*genpd
;
260 struct generic_pm_domain_data
*gpd_data
, *pd_data
;
261 struct pm_domain_data
*pdd
;
265 genpd
= dev_to_genpd(dev
);
269 if (unlikely(!genpd
->set_performance_state
))
272 if (unlikely(!dev
->power
.subsys_data
||
273 !dev
->power
.subsys_data
->domain_data
)) {
280 gpd_data
= to_gpd_data(dev
->power
.subsys_data
->domain_data
);
281 prev
= gpd_data
->performance_state
;
282 gpd_data
->performance_state
= state
;
284 /* New requested state is same as Max requested state */
285 if (state
== genpd
->performance_state
)
288 /* New requested state is higher than Max requested state */
289 if (state
> genpd
->performance_state
)
292 /* Traverse all devices within the domain */
293 list_for_each_entry(pdd
, &genpd
->dev_list
, list_node
) {
294 pd_data
= to_gpd_data(pdd
);
296 if (pd_data
->performance_state
> state
)
297 state
= pd_data
->performance_state
;
300 if (state
== genpd
->performance_state
)
304 * We aren't propagating performance state changes of a subdomain to its
305 * masters as we don't have hardware that needs it. Over that, the
306 * performance states of subdomain and its masters may not have
307 * one-to-one mapping and would require additional information. We can
308 * get back to this once we have hardware that needs it. For that
309 * reason, we don't have to consider performance state of the subdomains
314 if (genpd_status_on(genpd
)) {
315 ret
= genpd
->set_performance_state(genpd
, state
);
317 gpd_data
->performance_state
= prev
;
322 genpd
->performance_state
= state
;
329 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state
);
331 static int _genpd_power_on(struct generic_pm_domain
*genpd
, bool timed
)
333 unsigned int state_idx
= genpd
->state_idx
;
338 if (!genpd
->power_on
)
342 return genpd
->power_on(genpd
);
344 time_start
= ktime_get();
345 ret
= genpd
->power_on(genpd
);
349 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
351 if (unlikely(genpd
->set_performance_state
)) {
352 ret
= genpd
->set_performance_state(genpd
, genpd
->performance_state
);
354 pr_warn("%s: Failed to set performance state %d (%d)\n",
355 genpd
->name
, genpd
->performance_state
, ret
);
359 if (elapsed_ns
<= genpd
->states
[state_idx
].power_on_latency_ns
)
362 genpd
->states
[state_idx
].power_on_latency_ns
= elapsed_ns
;
363 genpd
->max_off_time_changed
= true;
364 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
365 genpd
->name
, "on", elapsed_ns
);
370 static int _genpd_power_off(struct generic_pm_domain
*genpd
, bool timed
)
372 unsigned int state_idx
= genpd
->state_idx
;
377 if (!genpd
->power_off
)
381 return genpd
->power_off(genpd
);
383 time_start
= ktime_get();
384 ret
= genpd
->power_off(genpd
);
388 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
389 if (elapsed_ns
<= genpd
->states
[state_idx
].power_off_latency_ns
)
392 genpd
->states
[state_idx
].power_off_latency_ns
= elapsed_ns
;
393 genpd
->max_off_time_changed
= true;
394 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
395 genpd
->name
, "off", elapsed_ns
);
401 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
402 * @genpd: PM domain to power off.
404 * Queue up the execution of genpd_power_off() unless it's already been done
407 static void genpd_queue_power_off_work(struct generic_pm_domain
*genpd
)
409 queue_work(pm_wq
, &genpd
->power_off_work
);
413 * genpd_power_off - Remove power from a given PM domain.
414 * @genpd: PM domain to power down.
415 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
416 * RPM status of the releated device is in an intermediate state, not yet turned
417 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
418 * be RPM_SUSPENDED, while it tries to power off the PM domain.
420 * If all of the @genpd's devices have been suspended and all of its subdomains
421 * have been powered down, remove power from @genpd.
423 static int genpd_power_off(struct generic_pm_domain
*genpd
, bool one_dev_on
,
426 struct pm_domain_data
*pdd
;
427 struct gpd_link
*link
;
428 unsigned int not_suspended
= 0;
431 * Do not try to power off the domain in the following situations:
432 * (1) The domain is already in the "power off" state.
433 * (2) System suspend is in progress.
435 if (!genpd_status_on(genpd
) || genpd
->prepared_count
> 0)
439 * Abort power off for the PM domain in the following situations:
440 * (1) The domain is configured as always on.
441 * (2) When the domain has a subdomain being powered on.
443 if (genpd_is_always_on(genpd
) || atomic_read(&genpd
->sd_count
) > 0)
446 list_for_each_entry(pdd
, &genpd
->dev_list
, list_node
) {
447 enum pm_qos_flags_status stat
;
449 stat
= dev_pm_qos_flags(pdd
->dev
, PM_QOS_FLAG_NO_POWER_OFF
);
450 if (stat
> PM_QOS_FLAGS_NONE
)
454 * Do not allow PM domain to be powered off, when an IRQ safe
455 * device is part of a non-IRQ safe domain.
457 if (!pm_runtime_suspended(pdd
->dev
) ||
458 irq_safe_dev_in_no_sleep_domain(pdd
->dev
, genpd
))
462 if (not_suspended
> 1 || (not_suspended
== 1 && !one_dev_on
))
465 if (genpd
->gov
&& genpd
->gov
->power_down_ok
) {
466 if (!genpd
->gov
->power_down_ok(&genpd
->domain
))
470 /* Default to shallowest state. */
472 genpd
->state_idx
= 0;
474 if (genpd
->power_off
) {
477 if (atomic_read(&genpd
->sd_count
) > 0)
481 * If sd_count > 0 at this point, one of the subdomains hasn't
482 * managed to call genpd_power_on() for the master yet after
483 * incrementing it. In that case genpd_power_on() will wait
484 * for us to drop the lock, so we can call .power_off() and let
485 * the genpd_power_on() restore power for us (this shouldn't
486 * happen very often).
488 ret
= _genpd_power_off(genpd
, true);
493 genpd
->status
= GPD_STATE_POWER_OFF
;
494 genpd_update_accounting(genpd
);
496 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
497 genpd_sd_counter_dec(link
->master
);
498 genpd_lock_nested(link
->master
, depth
+ 1);
499 genpd_power_off(link
->master
, false, depth
+ 1);
500 genpd_unlock(link
->master
);
507 * genpd_power_on - Restore power to a given PM domain and its masters.
508 * @genpd: PM domain to power up.
509 * @depth: nesting count for lockdep.
511 * Restore power to @genpd and all of its masters so that it is possible to
512 * resume a device belonging to it.
514 static int genpd_power_on(struct generic_pm_domain
*genpd
, unsigned int depth
)
516 struct gpd_link
*link
;
519 if (genpd_status_on(genpd
))
523 * The list is guaranteed not to change while the loop below is being
524 * executed, unless one of the masters' .power_on() callbacks fiddles
527 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
528 struct generic_pm_domain
*master
= link
->master
;
530 genpd_sd_counter_inc(master
);
532 genpd_lock_nested(master
, depth
+ 1);
533 ret
= genpd_power_on(master
, depth
+ 1);
534 genpd_unlock(master
);
537 genpd_sd_counter_dec(master
);
542 ret
= _genpd_power_on(genpd
, true);
546 genpd
->status
= GPD_STATE_ACTIVE
;
547 genpd_update_accounting(genpd
);
552 list_for_each_entry_continue_reverse(link
,
555 genpd_sd_counter_dec(link
->master
);
556 genpd_lock_nested(link
->master
, depth
+ 1);
557 genpd_power_off(link
->master
, false, depth
+ 1);
558 genpd_unlock(link
->master
);
564 static int genpd_dev_pm_qos_notifier(struct notifier_block
*nb
,
565 unsigned long val
, void *ptr
)
567 struct generic_pm_domain_data
*gpd_data
;
570 gpd_data
= container_of(nb
, struct generic_pm_domain_data
, nb
);
571 dev
= gpd_data
->base
.dev
;
574 struct generic_pm_domain
*genpd
;
575 struct pm_domain_data
*pdd
;
577 spin_lock_irq(&dev
->power
.lock
);
579 pdd
= dev
->power
.subsys_data
?
580 dev
->power
.subsys_data
->domain_data
: NULL
;
582 to_gpd_data(pdd
)->td
.constraint_changed
= true;
583 genpd
= dev_to_genpd(dev
);
585 genpd
= ERR_PTR(-ENODATA
);
588 spin_unlock_irq(&dev
->power
.lock
);
590 if (!IS_ERR(genpd
)) {
592 genpd
->max_off_time_changed
= true;
597 if (!dev
|| dev
->power
.ignore_children
)
605 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
606 * @work: Work structure used for scheduling the execution of this function.
608 static void genpd_power_off_work_fn(struct work_struct
*work
)
610 struct generic_pm_domain
*genpd
;
612 genpd
= container_of(work
, struct generic_pm_domain
, power_off_work
);
615 genpd_power_off(genpd
, false, 0);
620 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
621 * @dev: Device to handle.
623 static int __genpd_runtime_suspend(struct device
*dev
)
625 int (*cb
)(struct device
*__dev
);
627 if (dev
->type
&& dev
->type
->pm
)
628 cb
= dev
->type
->pm
->runtime_suspend
;
629 else if (dev
->class && dev
->class->pm
)
630 cb
= dev
->class->pm
->runtime_suspend
;
631 else if (dev
->bus
&& dev
->bus
->pm
)
632 cb
= dev
->bus
->pm
->runtime_suspend
;
636 if (!cb
&& dev
->driver
&& dev
->driver
->pm
)
637 cb
= dev
->driver
->pm
->runtime_suspend
;
639 return cb
? cb(dev
) : 0;
643 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
644 * @dev: Device to handle.
646 static int __genpd_runtime_resume(struct device
*dev
)
648 int (*cb
)(struct device
*__dev
);
650 if (dev
->type
&& dev
->type
->pm
)
651 cb
= dev
->type
->pm
->runtime_resume
;
652 else if (dev
->class && dev
->class->pm
)
653 cb
= dev
->class->pm
->runtime_resume
;
654 else if (dev
->bus
&& dev
->bus
->pm
)
655 cb
= dev
->bus
->pm
->runtime_resume
;
659 if (!cb
&& dev
->driver
&& dev
->driver
->pm
)
660 cb
= dev
->driver
->pm
->runtime_resume
;
662 return cb
? cb(dev
) : 0;
666 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
667 * @dev: Device to suspend.
669 * Carry out a runtime suspend of a device under the assumption that its
670 * pm_domain field points to the domain member of an object of type
671 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
673 static int genpd_runtime_suspend(struct device
*dev
)
675 struct generic_pm_domain
*genpd
;
676 bool (*suspend_ok
)(struct device
*__dev
);
677 struct gpd_timing_data
*td
= &dev_gpd_data(dev
)->td
;
678 bool runtime_pm
= pm_runtime_enabled(dev
);
683 dev_dbg(dev
, "%s()\n", __func__
);
685 genpd
= dev_to_genpd(dev
);
690 * A runtime PM centric subsystem/driver may re-use the runtime PM
691 * callbacks for other purposes than runtime PM. In those scenarios
692 * runtime PM is disabled. Under these circumstances, we shall skip
693 * validating/measuring the PM QoS latency.
695 suspend_ok
= genpd
->gov
? genpd
->gov
->suspend_ok
: NULL
;
696 if (runtime_pm
&& suspend_ok
&& !suspend_ok(dev
))
699 /* Measure suspend latency. */
702 time_start
= ktime_get();
704 ret
= __genpd_runtime_suspend(dev
);
708 ret
= genpd_stop_dev(genpd
, dev
);
710 __genpd_runtime_resume(dev
);
714 /* Update suspend latency value if the measured time exceeds it. */
716 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
717 if (elapsed_ns
> td
->suspend_latency_ns
) {
718 td
->suspend_latency_ns
= elapsed_ns
;
719 dev_dbg(dev
, "suspend latency exceeded, %lld ns\n",
721 genpd
->max_off_time_changed
= true;
722 td
->constraint_changed
= true;
727 * If power.irq_safe is set, this routine may be run with
728 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
730 if (irq_safe_dev_in_no_sleep_domain(dev
, genpd
))
734 genpd_power_off(genpd
, true, 0);
741 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
742 * @dev: Device to resume.
744 * Carry out a runtime resume of a device under the assumption that its
745 * pm_domain field points to the domain member of an object of type
746 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
748 static int genpd_runtime_resume(struct device
*dev
)
750 struct generic_pm_domain
*genpd
;
751 struct gpd_timing_data
*td
= &dev_gpd_data(dev
)->td
;
752 bool runtime_pm
= pm_runtime_enabled(dev
);
758 dev_dbg(dev
, "%s()\n", __func__
);
760 genpd
= dev_to_genpd(dev
);
765 * As we don't power off a non IRQ safe domain, which holds
766 * an IRQ safe device, we don't need to restore power to it.
768 if (irq_safe_dev_in_no_sleep_domain(dev
, genpd
)) {
774 ret
= genpd_power_on(genpd
, 0);
781 /* Measure resume latency. */
783 if (timed
&& runtime_pm
)
784 time_start
= ktime_get();
786 ret
= genpd_start_dev(genpd
, dev
);
790 ret
= __genpd_runtime_resume(dev
);
794 /* Update resume latency value if the measured time exceeds it. */
795 if (timed
&& runtime_pm
) {
796 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
797 if (elapsed_ns
> td
->resume_latency_ns
) {
798 td
->resume_latency_ns
= elapsed_ns
;
799 dev_dbg(dev
, "resume latency exceeded, %lld ns\n",
801 genpd
->max_off_time_changed
= true;
802 td
->constraint_changed
= true;
809 genpd_stop_dev(genpd
, dev
);
811 if (!pm_runtime_is_irq_safe(dev
) ||
812 (pm_runtime_is_irq_safe(dev
) && genpd_is_irq_safe(genpd
))) {
814 genpd_power_off(genpd
, true, 0);
821 static bool pd_ignore_unused
;
822 static int __init
pd_ignore_unused_setup(char *__unused
)
824 pd_ignore_unused
= true;
827 __setup("pd_ignore_unused", pd_ignore_unused_setup
);
830 * genpd_power_off_unused - Power off all PM domains with no devices in use.
832 static int __init
genpd_power_off_unused(void)
834 struct generic_pm_domain
*genpd
;
836 if (pd_ignore_unused
) {
837 pr_warn("genpd: Not disabling unused power domains\n");
841 mutex_lock(&gpd_list_lock
);
843 list_for_each_entry(genpd
, &gpd_list
, gpd_list_node
)
844 genpd_queue_power_off_work(genpd
);
846 mutex_unlock(&gpd_list_lock
);
850 late_initcall(genpd_power_off_unused
);
852 #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
854 static bool genpd_present(const struct generic_pm_domain
*genpd
)
856 const struct generic_pm_domain
*gpd
;
858 if (IS_ERR_OR_NULL(genpd
))
861 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
)
870 #ifdef CONFIG_PM_SLEEP
873 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
874 * @genpd: PM domain to power off, if possible.
875 * @use_lock: use the lock.
876 * @depth: nesting count for lockdep.
878 * Check if the given PM domain can be powered off (during system suspend or
879 * hibernation) and do that if so. Also, in that case propagate to its masters.
881 * This function is only called in "noirq" and "syscore" stages of system power
882 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
883 * these cases the lock must be held.
885 static void genpd_sync_power_off(struct generic_pm_domain
*genpd
, bool use_lock
,
888 struct gpd_link
*link
;
890 if (!genpd_status_on(genpd
) || genpd_is_always_on(genpd
))
893 if (genpd
->suspended_count
!= genpd
->device_count
894 || atomic_read(&genpd
->sd_count
) > 0)
897 /* Choose the deepest state when suspending */
898 genpd
->state_idx
= genpd
->state_count
- 1;
899 if (_genpd_power_off(genpd
, false))
902 genpd
->status
= GPD_STATE_POWER_OFF
;
904 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
905 genpd_sd_counter_dec(link
->master
);
908 genpd_lock_nested(link
->master
, depth
+ 1);
910 genpd_sync_power_off(link
->master
, use_lock
, depth
+ 1);
913 genpd_unlock(link
->master
);
918 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
919 * @genpd: PM domain to power on.
920 * @use_lock: use the lock.
921 * @depth: nesting count for lockdep.
923 * This function is only called in "noirq" and "syscore" stages of system power
924 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
925 * these cases the lock must be held.
927 static void genpd_sync_power_on(struct generic_pm_domain
*genpd
, bool use_lock
,
930 struct gpd_link
*link
;
932 if (genpd_status_on(genpd
))
935 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
936 genpd_sd_counter_inc(link
->master
);
939 genpd_lock_nested(link
->master
, depth
+ 1);
941 genpd_sync_power_on(link
->master
, use_lock
, depth
+ 1);
944 genpd_unlock(link
->master
);
947 _genpd_power_on(genpd
, false);
949 genpd
->status
= GPD_STATE_ACTIVE
;
953 * resume_needed - Check whether to resume a device before system suspend.
954 * @dev: Device to check.
955 * @genpd: PM domain the device belongs to.
957 * There are two cases in which a device that can wake up the system from sleep
958 * states should be resumed by genpd_prepare(): (1) if the device is enabled
959 * to wake up the system and it has to remain active for this purpose while the
960 * system is in the sleep state and (2) if the device is not enabled to wake up
961 * the system from sleep states and it generally doesn't generate wakeup signals
962 * by itself (those signals are generated on its behalf by other parts of the
963 * system). In the latter case it may be necessary to reconfigure the device's
964 * wakeup settings during system suspend, because it may have been set up to
965 * signal remote wakeup from the system's working state as needed by runtime PM.
966 * Return 'true' in either of the above cases.
968 static bool resume_needed(struct device
*dev
,
969 const struct generic_pm_domain
*genpd
)
973 if (!device_can_wakeup(dev
))
976 active_wakeup
= genpd_is_active_wakeup(genpd
);
977 return device_may_wakeup(dev
) ? active_wakeup
: !active_wakeup
;
981 * genpd_prepare - Start power transition of a device in a PM domain.
982 * @dev: Device to start the transition of.
984 * Start a power transition of a device (during a system-wide power transition)
985 * under the assumption that its pm_domain field points to the domain member of
986 * an object of type struct generic_pm_domain representing a PM domain
987 * consisting of I/O devices.
989 static int genpd_prepare(struct device
*dev
)
991 struct generic_pm_domain
*genpd
;
994 dev_dbg(dev
, "%s()\n", __func__
);
996 genpd
= dev_to_genpd(dev
);
1001 * If a wakeup request is pending for the device, it should be woken up
1002 * at this point and a system wakeup event should be reported if it's
1003 * set up to wake up the system from sleep states.
1005 if (resume_needed(dev
, genpd
))
1006 pm_runtime_resume(dev
);
1010 if (genpd
->prepared_count
++ == 0)
1011 genpd
->suspended_count
= 0;
1013 genpd_unlock(genpd
);
1015 ret
= pm_generic_prepare(dev
);
1019 genpd
->prepared_count
--;
1021 genpd_unlock(genpd
);
1024 /* Never return 1, as genpd don't cope with the direct_complete path. */
1025 return ret
>= 0 ? 0 : ret
;
1029 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1031 * @dev: Device to suspend.
1032 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
1034 * Stop the device and remove power from the domain if all devices in it have
1037 static int genpd_finish_suspend(struct device
*dev
, bool poweroff
)
1039 struct generic_pm_domain
*genpd
;
1042 genpd
= dev_to_genpd(dev
);
1047 ret
= pm_generic_poweroff_noirq(dev
);
1049 ret
= pm_generic_suspend_noirq(dev
);
1053 if (dev
->power
.wakeup_path
&& genpd_is_active_wakeup(genpd
))
1056 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
&&
1057 !pm_runtime_status_suspended(dev
)) {
1058 ret
= genpd_stop_dev(genpd
, dev
);
1061 pm_generic_restore_noirq(dev
);
1063 pm_generic_resume_noirq(dev
);
1069 genpd
->suspended_count
++;
1070 genpd_sync_power_off(genpd
, true, 0);
1071 genpd_unlock(genpd
);
1077 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1078 * @dev: Device to suspend.
1080 * Stop the device and remove power from the domain if all devices in it have
1083 static int genpd_suspend_noirq(struct device
*dev
)
1085 dev_dbg(dev
, "%s()\n", __func__
);
1087 return genpd_finish_suspend(dev
, false);
1091 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1092 * @dev: Device to resume.
1094 * Restore power to the device's PM domain, if necessary, and start the device.
1096 static int genpd_resume_noirq(struct device
*dev
)
1098 struct generic_pm_domain
*genpd
;
1101 dev_dbg(dev
, "%s()\n", __func__
);
1103 genpd
= dev_to_genpd(dev
);
1107 if (dev
->power
.wakeup_path
&& genpd_is_active_wakeup(genpd
))
1108 return pm_generic_resume_noirq(dev
);
1111 genpd_sync_power_on(genpd
, true, 0);
1112 genpd
->suspended_count
--;
1113 genpd_unlock(genpd
);
1115 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
&&
1116 !pm_runtime_status_suspended(dev
)) {
1117 ret
= genpd_start_dev(genpd
, dev
);
1122 return pm_generic_resume_noirq(dev
);
1126 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1127 * @dev: Device to freeze.
1129 * Carry out a late freeze of a device under the assumption that its
1130 * pm_domain field points to the domain member of an object of type
1131 * struct generic_pm_domain representing a power domain consisting of I/O
1134 static int genpd_freeze_noirq(struct device
*dev
)
1136 const struct generic_pm_domain
*genpd
;
1139 dev_dbg(dev
, "%s()\n", __func__
);
1141 genpd
= dev_to_genpd(dev
);
1145 ret
= pm_generic_freeze_noirq(dev
);
1149 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
&&
1150 !pm_runtime_status_suspended(dev
))
1151 ret
= genpd_stop_dev(genpd
, dev
);
1157 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1158 * @dev: Device to thaw.
1160 * Start the device, unless power has been removed from the domain already
1161 * before the system transition.
1163 static int genpd_thaw_noirq(struct device
*dev
)
1165 const struct generic_pm_domain
*genpd
;
1168 dev_dbg(dev
, "%s()\n", __func__
);
1170 genpd
= dev_to_genpd(dev
);
1174 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
&&
1175 !pm_runtime_status_suspended(dev
)) {
1176 ret
= genpd_start_dev(genpd
, dev
);
1181 return pm_generic_thaw_noirq(dev
);
1185 * genpd_poweroff_noirq - Completion of hibernation of device in an
1187 * @dev: Device to poweroff.
1189 * Stop the device and remove power from the domain if all devices in it have
1192 static int genpd_poweroff_noirq(struct device
*dev
)
1194 dev_dbg(dev
, "%s()\n", __func__
);
1196 return genpd_finish_suspend(dev
, true);
1200 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1201 * @dev: Device to resume.
1203 * Make sure the domain will be in the same power state as before the
1204 * hibernation the system is resuming from and start the device if necessary.
1206 static int genpd_restore_noirq(struct device
*dev
)
1208 struct generic_pm_domain
*genpd
;
1211 dev_dbg(dev
, "%s()\n", __func__
);
1213 genpd
= dev_to_genpd(dev
);
1218 * At this point suspended_count == 0 means we are being run for the
1219 * first time for the given domain in the present cycle.
1222 if (genpd
->suspended_count
++ == 0)
1224 * The boot kernel might put the domain into arbitrary state,
1225 * so make it appear as powered off to genpd_sync_power_on(),
1226 * so that it tries to power it on in case it was really off.
1228 genpd
->status
= GPD_STATE_POWER_OFF
;
1230 genpd_sync_power_on(genpd
, true, 0);
1231 genpd_unlock(genpd
);
1233 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
&&
1234 !pm_runtime_status_suspended(dev
)) {
1235 ret
= genpd_start_dev(genpd
, dev
);
1240 return pm_generic_restore_noirq(dev
);
1244 * genpd_complete - Complete power transition of a device in a power domain.
1245 * @dev: Device to complete the transition of.
1247 * Complete a power transition of a device (during a system-wide power
1248 * transition) under the assumption that its pm_domain field points to the
1249 * domain member of an object of type struct generic_pm_domain representing
1250 * a power domain consisting of I/O devices.
1252 static void genpd_complete(struct device
*dev
)
1254 struct generic_pm_domain
*genpd
;
1256 dev_dbg(dev
, "%s()\n", __func__
);
1258 genpd
= dev_to_genpd(dev
);
1262 pm_generic_complete(dev
);
1266 genpd
->prepared_count
--;
1267 if (!genpd
->prepared_count
)
1268 genpd_queue_power_off_work(genpd
);
1270 genpd_unlock(genpd
);
1274 * genpd_syscore_switch - Switch power during system core suspend or resume.
1275 * @dev: Device that normally is marked as "always on" to switch power for.
1277 * This routine may only be called during the system core (syscore) suspend or
1278 * resume phase for devices whose "always on" flags are set.
1280 static void genpd_syscore_switch(struct device
*dev
, bool suspend
)
1282 struct generic_pm_domain
*genpd
;
1284 genpd
= dev_to_genpd(dev
);
1285 if (!genpd_present(genpd
))
1289 genpd
->suspended_count
++;
1290 genpd_sync_power_off(genpd
, false, 0);
1292 genpd_sync_power_on(genpd
, false, 0);
1293 genpd
->suspended_count
--;
1297 void pm_genpd_syscore_poweroff(struct device
*dev
)
1299 genpd_syscore_switch(dev
, true);
1301 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff
);
1303 void pm_genpd_syscore_poweron(struct device
*dev
)
1305 genpd_syscore_switch(dev
, false);
1307 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron
);
1309 #else /* !CONFIG_PM_SLEEP */
1311 #define genpd_prepare NULL
1312 #define genpd_suspend_noirq NULL
1313 #define genpd_resume_noirq NULL
1314 #define genpd_freeze_noirq NULL
1315 #define genpd_thaw_noirq NULL
1316 #define genpd_poweroff_noirq NULL
1317 #define genpd_restore_noirq NULL
1318 #define genpd_complete NULL
1320 #endif /* CONFIG_PM_SLEEP */
1322 static struct generic_pm_domain_data
*genpd_alloc_dev_data(struct device
*dev
,
1323 struct gpd_timing_data
*td
)
1325 struct generic_pm_domain_data
*gpd_data
;
1328 ret
= dev_pm_get_subsys_data(dev
);
1330 return ERR_PTR(ret
);
1332 gpd_data
= kzalloc(sizeof(*gpd_data
), GFP_KERNEL
);
1341 gpd_data
->base
.dev
= dev
;
1342 gpd_data
->td
.constraint_changed
= true;
1343 gpd_data
->td
.effective_constraint_ns
= PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS
;
1344 gpd_data
->nb
.notifier_call
= genpd_dev_pm_qos_notifier
;
1346 spin_lock_irq(&dev
->power
.lock
);
1348 if (dev
->power
.subsys_data
->domain_data
) {
1353 dev
->power
.subsys_data
->domain_data
= &gpd_data
->base
;
1355 spin_unlock_irq(&dev
->power
.lock
);
1360 spin_unlock_irq(&dev
->power
.lock
);
1363 dev_pm_put_subsys_data(dev
);
1364 return ERR_PTR(ret
);
1367 static void genpd_free_dev_data(struct device
*dev
,
1368 struct generic_pm_domain_data
*gpd_data
)
1370 spin_lock_irq(&dev
->power
.lock
);
1372 dev
->power
.subsys_data
->domain_data
= NULL
;
1374 spin_unlock_irq(&dev
->power
.lock
);
1377 dev_pm_put_subsys_data(dev
);
1380 static int genpd_add_device(struct generic_pm_domain
*genpd
, struct device
*dev
,
1381 struct gpd_timing_data
*td
)
1383 struct generic_pm_domain_data
*gpd_data
;
1386 dev_dbg(dev
, "%s()\n", __func__
);
1388 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(dev
))
1391 gpd_data
= genpd_alloc_dev_data(dev
, td
);
1392 if (IS_ERR(gpd_data
))
1393 return PTR_ERR(gpd_data
);
1395 ret
= genpd
->attach_dev
? genpd
->attach_dev(genpd
, dev
) : 0;
1401 dev_pm_domain_set(dev
, &genpd
->domain
);
1403 genpd
->device_count
++;
1404 genpd
->max_off_time_changed
= true;
1406 list_add_tail(&gpd_data
->base
.list_node
, &genpd
->dev_list
);
1408 genpd_unlock(genpd
);
1411 genpd_free_dev_data(dev
, gpd_data
);
1413 dev_pm_qos_add_notifier(dev
, &gpd_data
->nb
);
1419 * pm_genpd_add_device - Add a device to an I/O PM domain.
1420 * @genpd: PM domain to add the device to.
1421 * @dev: Device to be added.
1423 int pm_genpd_add_device(struct generic_pm_domain
*genpd
, struct device
*dev
)
1427 mutex_lock(&gpd_list_lock
);
1428 ret
= genpd_add_device(genpd
, dev
, NULL
);
1429 mutex_unlock(&gpd_list_lock
);
1433 EXPORT_SYMBOL_GPL(pm_genpd_add_device
);
1435 static int genpd_remove_device(struct generic_pm_domain
*genpd
,
1438 struct generic_pm_domain_data
*gpd_data
;
1439 struct pm_domain_data
*pdd
;
1442 dev_dbg(dev
, "%s()\n", __func__
);
1444 pdd
= dev
->power
.subsys_data
->domain_data
;
1445 gpd_data
= to_gpd_data(pdd
);
1446 dev_pm_qos_remove_notifier(dev
, &gpd_data
->nb
);
1450 if (genpd
->prepared_count
> 0) {
1455 genpd
->device_count
--;
1456 genpd
->max_off_time_changed
= true;
1458 dev_pm_domain_set(dev
, NULL
);
1460 list_del_init(&pdd
->list_node
);
1462 genpd_unlock(genpd
);
1464 if (genpd
->detach_dev
)
1465 genpd
->detach_dev(genpd
, dev
);
1467 genpd_free_dev_data(dev
, gpd_data
);
1472 genpd_unlock(genpd
);
1473 dev_pm_qos_add_notifier(dev
, &gpd_data
->nb
);
1479 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1480 * @dev: Device to be removed.
1482 int pm_genpd_remove_device(struct device
*dev
)
1484 struct generic_pm_domain
*genpd
= genpd_lookup_dev(dev
);
1489 return genpd_remove_device(genpd
, dev
);
1491 EXPORT_SYMBOL_GPL(pm_genpd_remove_device
);
1493 static int genpd_add_subdomain(struct generic_pm_domain
*genpd
,
1494 struct generic_pm_domain
*subdomain
)
1496 struct gpd_link
*link
, *itr
;
1499 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(subdomain
)
1500 || genpd
== subdomain
)
1504 * If the domain can be powered on/off in an IRQ safe
1505 * context, ensure that the subdomain can also be
1506 * powered on/off in that context.
1508 if (!genpd_is_irq_safe(genpd
) && genpd_is_irq_safe(subdomain
)) {
1509 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1510 genpd
->name
, subdomain
->name
);
1514 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
1518 genpd_lock(subdomain
);
1519 genpd_lock_nested(genpd
, SINGLE_DEPTH_NESTING
);
1521 if (!genpd_status_on(genpd
) && genpd_status_on(subdomain
)) {
1526 list_for_each_entry(itr
, &genpd
->master_links
, master_node
) {
1527 if (itr
->slave
== subdomain
&& itr
->master
== genpd
) {
1533 link
->master
= genpd
;
1534 list_add_tail(&link
->master_node
, &genpd
->master_links
);
1535 link
->slave
= subdomain
;
1536 list_add_tail(&link
->slave_node
, &subdomain
->slave_links
);
1537 if (genpd_status_on(subdomain
))
1538 genpd_sd_counter_inc(genpd
);
1541 genpd_unlock(genpd
);
1542 genpd_unlock(subdomain
);
1549 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1550 * @genpd: Master PM domain to add the subdomain to.
1551 * @subdomain: Subdomain to be added.
1553 int pm_genpd_add_subdomain(struct generic_pm_domain
*genpd
,
1554 struct generic_pm_domain
*subdomain
)
1558 mutex_lock(&gpd_list_lock
);
1559 ret
= genpd_add_subdomain(genpd
, subdomain
);
1560 mutex_unlock(&gpd_list_lock
);
1564 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain
);
1567 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1568 * @genpd: Master PM domain to remove the subdomain from.
1569 * @subdomain: Subdomain to be removed.
1571 int pm_genpd_remove_subdomain(struct generic_pm_domain
*genpd
,
1572 struct generic_pm_domain
*subdomain
)
1574 struct gpd_link
*l
, *link
;
1577 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(subdomain
))
1580 genpd_lock(subdomain
);
1581 genpd_lock_nested(genpd
, SINGLE_DEPTH_NESTING
);
1583 if (!list_empty(&subdomain
->master_links
) || subdomain
->device_count
) {
1584 pr_warn("%s: unable to remove subdomain %s\n", genpd
->name
,
1590 list_for_each_entry_safe(link
, l
, &genpd
->master_links
, master_node
) {
1591 if (link
->slave
!= subdomain
)
1594 list_del(&link
->master_node
);
1595 list_del(&link
->slave_node
);
1597 if (genpd_status_on(subdomain
))
1598 genpd_sd_counter_dec(genpd
);
1605 genpd_unlock(genpd
);
1606 genpd_unlock(subdomain
);
1610 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain
);
1612 static int genpd_set_default_power_state(struct generic_pm_domain
*genpd
)
1614 struct genpd_power_state
*state
;
1616 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1620 genpd
->states
= state
;
1621 genpd
->state_count
= 1;
1622 genpd
->free
= state
;
1627 static void genpd_lock_init(struct generic_pm_domain
*genpd
)
1629 if (genpd
->flags
& GENPD_FLAG_IRQ_SAFE
) {
1630 spin_lock_init(&genpd
->slock
);
1631 genpd
->lock_ops
= &genpd_spin_ops
;
1633 mutex_init(&genpd
->mlock
);
1634 genpd
->lock_ops
= &genpd_mtx_ops
;
1639 * pm_genpd_init - Initialize a generic I/O PM domain object.
1640 * @genpd: PM domain object to initialize.
1641 * @gov: PM domain governor to associate with the domain (may be NULL).
1642 * @is_off: Initial value of the domain's power_is_off field.
1644 * Returns 0 on successful initialization, else a negative error code.
1646 int pm_genpd_init(struct generic_pm_domain
*genpd
,
1647 struct dev_power_governor
*gov
, bool is_off
)
1651 if (IS_ERR_OR_NULL(genpd
))
1654 INIT_LIST_HEAD(&genpd
->master_links
);
1655 INIT_LIST_HEAD(&genpd
->slave_links
);
1656 INIT_LIST_HEAD(&genpd
->dev_list
);
1657 genpd_lock_init(genpd
);
1659 INIT_WORK(&genpd
->power_off_work
, genpd_power_off_work_fn
);
1660 atomic_set(&genpd
->sd_count
, 0);
1661 genpd
->status
= is_off
? GPD_STATE_POWER_OFF
: GPD_STATE_ACTIVE
;
1662 genpd
->device_count
= 0;
1663 genpd
->max_off_time_ns
= -1;
1664 genpd
->max_off_time_changed
= true;
1665 genpd
->provider
= NULL
;
1666 genpd
->has_provider
= false;
1667 genpd
->accounting_time
= ktime_get();
1668 genpd
->domain
.ops
.runtime_suspend
= genpd_runtime_suspend
;
1669 genpd
->domain
.ops
.runtime_resume
= genpd_runtime_resume
;
1670 genpd
->domain
.ops
.prepare
= genpd_prepare
;
1671 genpd
->domain
.ops
.suspend_noirq
= genpd_suspend_noirq
;
1672 genpd
->domain
.ops
.resume_noirq
= genpd_resume_noirq
;
1673 genpd
->domain
.ops
.freeze_noirq
= genpd_freeze_noirq
;
1674 genpd
->domain
.ops
.thaw_noirq
= genpd_thaw_noirq
;
1675 genpd
->domain
.ops
.poweroff_noirq
= genpd_poweroff_noirq
;
1676 genpd
->domain
.ops
.restore_noirq
= genpd_restore_noirq
;
1677 genpd
->domain
.ops
.complete
= genpd_complete
;
1679 if (genpd
->flags
& GENPD_FLAG_PM_CLK
) {
1680 genpd
->dev_ops
.stop
= pm_clk_suspend
;
1681 genpd
->dev_ops
.start
= pm_clk_resume
;
1684 /* Always-on domains must be powered on at initialization. */
1685 if (genpd_is_always_on(genpd
) && !genpd_status_on(genpd
))
1688 /* Use only one "off" state if there were no states declared */
1689 if (genpd
->state_count
== 0) {
1690 ret
= genpd_set_default_power_state(genpd
);
1694 pr_warn("%s : no governor for states\n", genpd
->name
);
1697 device_initialize(&genpd
->dev
);
1698 dev_set_name(&genpd
->dev
, "%s", genpd
->name
);
1700 mutex_lock(&gpd_list_lock
);
1701 list_add(&genpd
->gpd_list_node
, &gpd_list
);
1702 mutex_unlock(&gpd_list_lock
);
1706 EXPORT_SYMBOL_GPL(pm_genpd_init
);
1708 static int genpd_remove(struct generic_pm_domain
*genpd
)
1710 struct gpd_link
*l
, *link
;
1712 if (IS_ERR_OR_NULL(genpd
))
1717 if (genpd
->has_provider
) {
1718 genpd_unlock(genpd
);
1719 pr_err("Provider present, unable to remove %s\n", genpd
->name
);
1723 if (!list_empty(&genpd
->master_links
) || genpd
->device_count
) {
1724 genpd_unlock(genpd
);
1725 pr_err("%s: unable to remove %s\n", __func__
, genpd
->name
);
1729 list_for_each_entry_safe(link
, l
, &genpd
->slave_links
, slave_node
) {
1730 list_del(&link
->master_node
);
1731 list_del(&link
->slave_node
);
1735 list_del(&genpd
->gpd_list_node
);
1736 genpd_unlock(genpd
);
1737 cancel_work_sync(&genpd
->power_off_work
);
1739 pr_debug("%s: removed %s\n", __func__
, genpd
->name
);
1745 * pm_genpd_remove - Remove a generic I/O PM domain
1746 * @genpd: Pointer to PM domain that is to be removed.
1748 * To remove the PM domain, this function:
1749 * - Removes the PM domain as a subdomain to any parent domains,
1751 * - Removes the PM domain from the list of registered PM domains.
1753 * The PM domain will only be removed, if the associated provider has
1754 * been removed, it is not a parent to any other PM domain and has no
1755 * devices associated with it.
1757 int pm_genpd_remove(struct generic_pm_domain
*genpd
)
1761 mutex_lock(&gpd_list_lock
);
1762 ret
= genpd_remove(genpd
);
1763 mutex_unlock(&gpd_list_lock
);
1767 EXPORT_SYMBOL_GPL(pm_genpd_remove
);
1769 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1772 * Device Tree based PM domain providers.
1774 * The code below implements generic device tree based PM domain providers that
1775 * bind device tree nodes with generic PM domains registered in the system.
1777 * Any driver that registers generic PM domains and needs to support binding of
1778 * devices to these domains is supposed to register a PM domain provider, which
1779 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1781 * Two simple mapping functions have been provided for convenience:
1782 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1783 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1788 * struct of_genpd_provider - PM domain provider registration structure
1789 * @link: Entry in global list of PM domain providers
1790 * @node: Pointer to device tree node of PM domain provider
1791 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1793 * @data: context pointer to be passed into @xlate callback
1795 struct of_genpd_provider
{
1796 struct list_head link
;
1797 struct device_node
*node
;
1798 genpd_xlate_t xlate
;
1802 /* List of registered PM domain providers. */
1803 static LIST_HEAD(of_genpd_providers
);
1804 /* Mutex to protect the list above. */
1805 static DEFINE_MUTEX(of_genpd_mutex
);
1808 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1809 * @genpdspec: OF phandle args to map into a PM domain
1810 * @data: xlate function private data - pointer to struct generic_pm_domain
1812 * This is a generic xlate function that can be used to model PM domains that
1813 * have their own device tree nodes. The private data of xlate function needs
1814 * to be a valid pointer to struct generic_pm_domain.
1816 static struct generic_pm_domain
*genpd_xlate_simple(
1817 struct of_phandle_args
*genpdspec
,
1824 * genpd_xlate_onecell() - Xlate function using a single index.
1825 * @genpdspec: OF phandle args to map into a PM domain
1826 * @data: xlate function private data - pointer to struct genpd_onecell_data
1828 * This is a generic xlate function that can be used to model simple PM domain
1829 * controllers that have one device tree node and provide multiple PM domains.
1830 * A single cell is used as an index into an array of PM domains specified in
1831 * the genpd_onecell_data struct when registering the provider.
1833 static struct generic_pm_domain
*genpd_xlate_onecell(
1834 struct of_phandle_args
*genpdspec
,
1837 struct genpd_onecell_data
*genpd_data
= data
;
1838 unsigned int idx
= genpdspec
->args
[0];
1840 if (genpdspec
->args_count
!= 1)
1841 return ERR_PTR(-EINVAL
);
1843 if (idx
>= genpd_data
->num_domains
) {
1844 pr_err("%s: invalid domain index %u\n", __func__
, idx
);
1845 return ERR_PTR(-EINVAL
);
1848 if (!genpd_data
->domains
[idx
])
1849 return ERR_PTR(-ENOENT
);
1851 return genpd_data
->domains
[idx
];
1855 * genpd_add_provider() - Register a PM domain provider for a node
1856 * @np: Device node pointer associated with the PM domain provider.
1857 * @xlate: Callback for decoding PM domain from phandle arguments.
1858 * @data: Context pointer for @xlate callback.
1860 static int genpd_add_provider(struct device_node
*np
, genpd_xlate_t xlate
,
1863 struct of_genpd_provider
*cp
;
1865 cp
= kzalloc(sizeof(*cp
), GFP_KERNEL
);
1869 cp
->node
= of_node_get(np
);
1873 mutex_lock(&of_genpd_mutex
);
1874 list_add(&cp
->link
, &of_genpd_providers
);
1875 mutex_unlock(&of_genpd_mutex
);
1876 pr_debug("Added domain provider from %pOF\n", np
);
1882 * of_genpd_add_provider_simple() - Register a simple PM domain provider
1883 * @np: Device node pointer associated with the PM domain provider.
1884 * @genpd: Pointer to PM domain associated with the PM domain provider.
1886 int of_genpd_add_provider_simple(struct device_node
*np
,
1887 struct generic_pm_domain
*genpd
)
1894 mutex_lock(&gpd_list_lock
);
1896 if (!genpd_present(genpd
))
1899 genpd
->dev
.of_node
= np
;
1901 /* Parse genpd OPP table */
1902 if (genpd
->set_performance_state
) {
1903 ret
= dev_pm_opp_of_add_table(&genpd
->dev
);
1905 dev_err(&genpd
->dev
, "Failed to add OPP table: %d\n",
1911 ret
= genpd_add_provider(np
, genpd_xlate_simple
, genpd
);
1913 if (genpd
->set_performance_state
)
1914 dev_pm_opp_of_remove_table(&genpd
->dev
);
1919 genpd
->provider
= &np
->fwnode
;
1920 genpd
->has_provider
= true;
1923 mutex_unlock(&gpd_list_lock
);
1927 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple
);
1930 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
1931 * @np: Device node pointer associated with the PM domain provider.
1932 * @data: Pointer to the data associated with the PM domain provider.
1934 int of_genpd_add_provider_onecell(struct device_node
*np
,
1935 struct genpd_onecell_data
*data
)
1937 struct generic_pm_domain
*genpd
;
1944 mutex_lock(&gpd_list_lock
);
1947 data
->xlate
= genpd_xlate_onecell
;
1949 for (i
= 0; i
< data
->num_domains
; i
++) {
1950 genpd
= data
->domains
[i
];
1954 if (!genpd_present(genpd
))
1957 genpd
->dev
.of_node
= np
;
1959 /* Parse genpd OPP table */
1960 if (genpd
->set_performance_state
) {
1961 ret
= dev_pm_opp_of_add_table_indexed(&genpd
->dev
, i
);
1963 dev_err(&genpd
->dev
, "Failed to add OPP table for index %d: %d\n",
1969 genpd
->provider
= &np
->fwnode
;
1970 genpd
->has_provider
= true;
1973 ret
= genpd_add_provider(np
, data
->xlate
, data
);
1977 mutex_unlock(&gpd_list_lock
);
1983 genpd
= data
->domains
[i
];
1988 genpd
->provider
= NULL
;
1989 genpd
->has_provider
= false;
1991 if (genpd
->set_performance_state
)
1992 dev_pm_opp_of_remove_table(&genpd
->dev
);
1995 mutex_unlock(&gpd_list_lock
);
1999 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell
);
2002 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2003 * @np: Device node pointer associated with the PM domain provider
2005 void of_genpd_del_provider(struct device_node
*np
)
2007 struct of_genpd_provider
*cp
, *tmp
;
2008 struct generic_pm_domain
*gpd
;
2010 mutex_lock(&gpd_list_lock
);
2011 mutex_lock(&of_genpd_mutex
);
2012 list_for_each_entry_safe(cp
, tmp
, &of_genpd_providers
, link
) {
2013 if (cp
->node
== np
) {
2015 * For each PM domain associated with the
2016 * provider, set the 'has_provider' to false
2017 * so that the PM domain can be safely removed.
2019 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
) {
2020 if (gpd
->provider
== &np
->fwnode
) {
2021 gpd
->has_provider
= false;
2023 if (!gpd
->set_performance_state
)
2026 dev_pm_opp_of_remove_table(&gpd
->dev
);
2030 list_del(&cp
->link
);
2031 of_node_put(cp
->node
);
2036 mutex_unlock(&of_genpd_mutex
);
2037 mutex_unlock(&gpd_list_lock
);
2039 EXPORT_SYMBOL_GPL(of_genpd_del_provider
);
2042 * genpd_get_from_provider() - Look-up PM domain
2043 * @genpdspec: OF phandle args to use for look-up
2045 * Looks for a PM domain provider under the node specified by @genpdspec and if
2046 * found, uses xlate function of the provider to map phandle args to a PM
2049 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2052 static struct generic_pm_domain
*genpd_get_from_provider(
2053 struct of_phandle_args
*genpdspec
)
2055 struct generic_pm_domain
*genpd
= ERR_PTR(-ENOENT
);
2056 struct of_genpd_provider
*provider
;
2059 return ERR_PTR(-EINVAL
);
2061 mutex_lock(&of_genpd_mutex
);
2063 /* Check if we have such a provider in our array */
2064 list_for_each_entry(provider
, &of_genpd_providers
, link
) {
2065 if (provider
->node
== genpdspec
->np
)
2066 genpd
= provider
->xlate(genpdspec
, provider
->data
);
2071 mutex_unlock(&of_genpd_mutex
);
2077 * of_genpd_add_device() - Add a device to an I/O PM domain
2078 * @genpdspec: OF phandle args to use for look-up PM domain
2079 * @dev: Device to be added.
2081 * Looks-up an I/O PM domain based upon phandle args provided and adds
2082 * the device to the PM domain. Returns a negative error code on failure.
2084 int of_genpd_add_device(struct of_phandle_args
*genpdspec
, struct device
*dev
)
2086 struct generic_pm_domain
*genpd
;
2089 mutex_lock(&gpd_list_lock
);
2091 genpd
= genpd_get_from_provider(genpdspec
);
2092 if (IS_ERR(genpd
)) {
2093 ret
= PTR_ERR(genpd
);
2097 ret
= genpd_add_device(genpd
, dev
, NULL
);
2100 mutex_unlock(&gpd_list_lock
);
2104 EXPORT_SYMBOL_GPL(of_genpd_add_device
);
2107 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2108 * @parent_spec: OF phandle args to use for parent PM domain look-up
2109 * @subdomain_spec: OF phandle args to use for subdomain look-up
2111 * Looks-up a parent PM domain and subdomain based upon phandle args
2112 * provided and adds the subdomain to the parent PM domain. Returns a
2113 * negative error code on failure.
2115 int of_genpd_add_subdomain(struct of_phandle_args
*parent_spec
,
2116 struct of_phandle_args
*subdomain_spec
)
2118 struct generic_pm_domain
*parent
, *subdomain
;
2121 mutex_lock(&gpd_list_lock
);
2123 parent
= genpd_get_from_provider(parent_spec
);
2124 if (IS_ERR(parent
)) {
2125 ret
= PTR_ERR(parent
);
2129 subdomain
= genpd_get_from_provider(subdomain_spec
);
2130 if (IS_ERR(subdomain
)) {
2131 ret
= PTR_ERR(subdomain
);
2135 ret
= genpd_add_subdomain(parent
, subdomain
);
2138 mutex_unlock(&gpd_list_lock
);
2142 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain
);
2145 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2146 * @provider: Pointer to device structure associated with provider
2148 * Find the last PM domain that was added by a particular provider and
2149 * remove this PM domain from the list of PM domains. The provider is
2150 * identified by the 'provider' device structure that is passed. The PM
2151 * domain will only be removed, if the provider associated with domain
2154 * Returns a valid pointer to struct generic_pm_domain on success or
2155 * ERR_PTR() on failure.
2157 struct generic_pm_domain
*of_genpd_remove_last(struct device_node
*np
)
2159 struct generic_pm_domain
*gpd
, *tmp
, *genpd
= ERR_PTR(-ENOENT
);
2162 if (IS_ERR_OR_NULL(np
))
2163 return ERR_PTR(-EINVAL
);
2165 mutex_lock(&gpd_list_lock
);
2166 list_for_each_entry_safe(gpd
, tmp
, &gpd_list
, gpd_list_node
) {
2167 if (gpd
->provider
== &np
->fwnode
) {
2168 ret
= genpd_remove(gpd
);
2169 genpd
= ret
? ERR_PTR(ret
) : gpd
;
2173 mutex_unlock(&gpd_list_lock
);
2177 EXPORT_SYMBOL_GPL(of_genpd_remove_last
);
2179 static void genpd_release_dev(struct device
*dev
)
2184 static struct bus_type genpd_bus_type
= {
2189 * genpd_dev_pm_detach - Detach a device from its PM domain.
2190 * @dev: Device to detach.
2191 * @power_off: Currently not used
2193 * Try to locate a corresponding generic PM domain, which the device was
2194 * attached to previously. If such is found, the device is detached from it.
2196 static void genpd_dev_pm_detach(struct device
*dev
, bool power_off
)
2198 struct generic_pm_domain
*pd
;
2202 pd
= dev_to_genpd(dev
);
2206 dev_dbg(dev
, "removing from PM domain %s\n", pd
->name
);
2208 for (i
= 1; i
< GENPD_RETRY_MAX_MS
; i
<<= 1) {
2209 ret
= genpd_remove_device(pd
, dev
);
2218 dev_err(dev
, "failed to remove from PM domain %s: %d",
2223 /* Check if PM domain can be powered off after removing this device. */
2224 genpd_queue_power_off_work(pd
);
2226 /* Unregister the device if it was created by genpd. */
2227 if (dev
->bus
== &genpd_bus_type
)
2228 device_unregister(dev
);
2231 static void genpd_dev_pm_sync(struct device
*dev
)
2233 struct generic_pm_domain
*pd
;
2235 pd
= dev_to_genpd(dev
);
2239 genpd_queue_power_off_work(pd
);
2242 static int __genpd_dev_pm_attach(struct device
*dev
, struct device_node
*np
,
2243 unsigned int index
, bool power_on
)
2245 struct of_phandle_args pd_args
;
2246 struct generic_pm_domain
*pd
;
2249 ret
= of_parse_phandle_with_args(np
, "power-domains",
2250 "#power-domain-cells", index
, &pd_args
);
2254 mutex_lock(&gpd_list_lock
);
2255 pd
= genpd_get_from_provider(&pd_args
);
2256 of_node_put(pd_args
.np
);
2258 mutex_unlock(&gpd_list_lock
);
2259 dev_dbg(dev
, "%s() failed to find PM domain: %ld\n",
2260 __func__
, PTR_ERR(pd
));
2261 return driver_deferred_probe_check_state(dev
);
2264 dev_dbg(dev
, "adding to PM domain %s\n", pd
->name
);
2266 ret
= genpd_add_device(pd
, dev
, NULL
);
2267 mutex_unlock(&gpd_list_lock
);
2270 if (ret
!= -EPROBE_DEFER
)
2271 dev_err(dev
, "failed to add to PM domain %s: %d",
2276 dev
->pm_domain
->detach
= genpd_dev_pm_detach
;
2277 dev
->pm_domain
->sync
= genpd_dev_pm_sync
;
2281 ret
= genpd_power_on(pd
, 0);
2286 genpd_remove_device(pd
, dev
);
2288 return ret
? -EPROBE_DEFER
: 1;
2292 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2293 * @dev: Device to attach.
2295 * Parse device's OF node to find a PM domain specifier. If such is found,
2296 * attaches the device to retrieved pm_domain ops.
2298 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2299 * PM domain or when multiple power-domains exists for it, else a negative error
2300 * code. Note that if a power-domain exists for the device, but it cannot be
2301 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2302 * not probed and to re-try again later.
2304 int genpd_dev_pm_attach(struct device
*dev
)
2310 * Devices with multiple PM domains must be attached separately, as we
2311 * can only attach one PM domain per device.
2313 if (of_count_phandle_with_args(dev
->of_node
, "power-domains",
2314 "#power-domain-cells") != 1)
2317 return __genpd_dev_pm_attach(dev
, dev
->of_node
, 0, true);
2319 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach
);
2322 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2323 * @dev: The device used to lookup the PM domain.
2324 * @index: The index of the PM domain.
2326 * Parse device's OF node to find a PM domain specifier at the provided @index.
2327 * If such is found, creates a virtual device and attaches it to the retrieved
2328 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2329 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2331 * Returns the created virtual device if successfully attached PM domain, NULL
2332 * when the device don't need a PM domain, else an ERR_PTR() in case of
2333 * failures. If a power-domain exists for the device, but cannot be found or
2334 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2335 * is not probed and to re-try again later.
2337 struct device
*genpd_dev_pm_attach_by_id(struct device
*dev
,
2340 struct device
*genpd_dev
;
2347 /* Deal only with devices using multiple PM domains. */
2348 num_domains
= of_count_phandle_with_args(dev
->of_node
, "power-domains",
2349 "#power-domain-cells");
2350 if (num_domains
< 2 || index
>= num_domains
)
2353 /* Allocate and register device on the genpd bus. */
2354 genpd_dev
= kzalloc(sizeof(*genpd_dev
), GFP_KERNEL
);
2356 return ERR_PTR(-ENOMEM
);
2358 dev_set_name(genpd_dev
, "genpd:%u:%s", index
, dev_name(dev
));
2359 genpd_dev
->bus
= &genpd_bus_type
;
2360 genpd_dev
->release
= genpd_release_dev
;
2362 ret
= device_register(genpd_dev
);
2365 return ERR_PTR(ret
);
2368 /* Try to attach the device to the PM domain at the specified index. */
2369 ret
= __genpd_dev_pm_attach(genpd_dev
, dev
->of_node
, index
, false);
2371 device_unregister(genpd_dev
);
2372 return ret
? ERR_PTR(ret
) : NULL
;
2375 pm_runtime_enable(genpd_dev
);
2376 genpd_queue_power_off_work(dev_to_genpd(genpd_dev
));
2380 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id
);
2383 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
2384 * @dev: The device used to lookup the PM domain.
2385 * @name: The name of the PM domain.
2387 * Parse device's OF node to find a PM domain specifier using the
2388 * power-domain-names DT property. For further description see
2389 * genpd_dev_pm_attach_by_id().
2391 struct device
*genpd_dev_pm_attach_by_name(struct device
*dev
, char *name
)
2398 index
= of_property_match_string(dev
->of_node
, "power-domain-names",
2403 return genpd_dev_pm_attach_by_id(dev
, index
);
2406 static const struct of_device_id idle_state_match
[] = {
2407 { .compatible
= "domain-idle-state", },
2411 static int genpd_parse_state(struct genpd_power_state
*genpd_state
,
2412 struct device_node
*state_node
)
2416 u32 entry_latency
, exit_latency
;
2418 err
= of_property_read_u32(state_node
, "entry-latency-us",
2421 pr_debug(" * %pOF missing entry-latency-us property\n",
2426 err
= of_property_read_u32(state_node
, "exit-latency-us",
2429 pr_debug(" * %pOF missing exit-latency-us property\n",
2434 err
= of_property_read_u32(state_node
, "min-residency-us", &residency
);
2436 genpd_state
->residency_ns
= 1000 * residency
;
2438 genpd_state
->power_on_latency_ns
= 1000 * exit_latency
;
2439 genpd_state
->power_off_latency_ns
= 1000 * entry_latency
;
2440 genpd_state
->fwnode
= &state_node
->fwnode
;
2445 static int genpd_iterate_idle_states(struct device_node
*dn
,
2446 struct genpd_power_state
*states
)
2449 struct of_phandle_iterator it
;
2450 struct device_node
*np
;
2453 ret
= of_count_phandle_with_args(dn
, "domain-idle-states", NULL
);
2457 /* Loop over the phandles until all the requested entry is found */
2458 of_for_each_phandle(&it
, ret
, dn
, "domain-idle-states", NULL
, 0) {
2460 if (!of_match_node(idle_state_match
, np
))
2463 ret
= genpd_parse_state(&states
[i
], np
);
2465 pr_err("Parsing idle state node %pOF failed with err %d\n",
2478 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2480 * @dn: The genpd device node
2481 * @states: The pointer to which the state array will be saved.
2482 * @n: The count of elements in the array returned from this function.
2484 * Returns the device states parsed from the OF node. The memory for the states
2485 * is allocated by this function and is the responsibility of the caller to
2486 * free the memory after use. If no domain idle states is found it returns
2487 * -EINVAL and in case of errors, a negative error code.
2489 int of_genpd_parse_idle_states(struct device_node
*dn
,
2490 struct genpd_power_state
**states
, int *n
)
2492 struct genpd_power_state
*st
;
2495 ret
= genpd_iterate_idle_states(dn
, NULL
);
2497 return ret
< 0 ? ret
: -EINVAL
;
2499 st
= kcalloc(ret
, sizeof(*st
), GFP_KERNEL
);
2503 ret
= genpd_iterate_idle_states(dn
, st
);
2506 return ret
< 0 ? ret
: -EINVAL
;
2514 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states
);
2517 * of_genpd_opp_to_performance_state- Gets performance state of device's
2518 * power domain corresponding to a DT node's "required-opps" property.
2520 * @dev: Device for which the performance-state needs to be found.
2521 * @np: DT node where the "required-opps" property is present. This can be
2522 * the device node itself (if it doesn't have an OPP table) or a node
2523 * within the OPP table of a device (if device has an OPP table).
2525 * Returns performance state corresponding to the "required-opps" property of
2526 * a DT node. This calls platform specific genpd->opp_to_performance_state()
2527 * callback to translate power domain OPP to performance state.
2529 * Returns performance state on success and 0 on failure.
2531 unsigned int of_genpd_opp_to_performance_state(struct device
*dev
,
2532 struct device_node
*np
)
2534 struct generic_pm_domain
*genpd
;
2535 struct dev_pm_opp
*opp
;
2538 genpd
= dev_to_genpd(dev
);
2542 if (unlikely(!genpd
->set_performance_state
))
2547 opp
= of_dev_pm_opp_find_required_opp(&genpd
->dev
, np
);
2549 dev_err(dev
, "Failed to find required OPP: %ld\n",
2554 state
= genpd
->opp_to_performance_state(genpd
, opp
);
2555 dev_pm_opp_put(opp
);
2558 genpd_unlock(genpd
);
2562 EXPORT_SYMBOL_GPL(of_genpd_opp_to_performance_state
);
2564 static int __init
genpd_bus_init(void)
2566 return bus_register(&genpd_bus_type
);
2568 core_initcall(genpd_bus_init
);
2570 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2573 /*** debugfs support ***/
2575 #ifdef CONFIG_DEBUG_FS
2576 #include <linux/pm.h>
2577 #include <linux/device.h>
2578 #include <linux/debugfs.h>
2579 #include <linux/seq_file.h>
2580 #include <linux/init.h>
2581 #include <linux/kobject.h>
2582 static struct dentry
*genpd_debugfs_dir
;
2585 * TODO: This function is a slightly modified version of rtpm_status_show
2586 * from sysfs.c, so generalize it.
2588 static void rtpm_status_str(struct seq_file
*s
, struct device
*dev
)
2590 static const char * const status_lookup
[] = {
2591 [RPM_ACTIVE
] = "active",
2592 [RPM_RESUMING
] = "resuming",
2593 [RPM_SUSPENDED
] = "suspended",
2594 [RPM_SUSPENDING
] = "suspending"
2598 if (dev
->power
.runtime_error
)
2600 else if (dev
->power
.disable_depth
)
2602 else if (dev
->power
.runtime_status
< ARRAY_SIZE(status_lookup
))
2603 p
= status_lookup
[dev
->power
.runtime_status
];
2610 static int genpd_summary_one(struct seq_file
*s
,
2611 struct generic_pm_domain
*genpd
)
2613 static const char * const status_lookup
[] = {
2614 [GPD_STATE_ACTIVE
] = "on",
2615 [GPD_STATE_POWER_OFF
] = "off"
2617 struct pm_domain_data
*pm_data
;
2618 const char *kobj_path
;
2619 struct gpd_link
*link
;
2623 ret
= genpd_lock_interruptible(genpd
);
2625 return -ERESTARTSYS
;
2627 if (WARN_ON(genpd
->status
>= ARRAY_SIZE(status_lookup
)))
2629 if (!genpd_status_on(genpd
))
2630 snprintf(state
, sizeof(state
), "%s-%u",
2631 status_lookup
[genpd
->status
], genpd
->state_idx
);
2633 snprintf(state
, sizeof(state
), "%s",
2634 status_lookup
[genpd
->status
]);
2635 seq_printf(s
, "%-30s %-15s ", genpd
->name
, state
);
2638 * Modifications on the list require holding locks on both
2639 * master and slave, so we are safe.
2640 * Also genpd->name is immutable.
2642 list_for_each_entry(link
, &genpd
->master_links
, master_node
) {
2643 seq_printf(s
, "%s", link
->slave
->name
);
2644 if (!list_is_last(&link
->master_node
, &genpd
->master_links
))
2648 list_for_each_entry(pm_data
, &genpd
->dev_list
, list_node
) {
2649 kobj_path
= kobject_get_path(&pm_data
->dev
->kobj
,
2650 genpd_is_irq_safe(genpd
) ?
2651 GFP_ATOMIC
: GFP_KERNEL
);
2652 if (kobj_path
== NULL
)
2655 seq_printf(s
, "\n %-50s ", kobj_path
);
2656 rtpm_status_str(s
, pm_data
->dev
);
2662 genpd_unlock(genpd
);
2667 static int genpd_summary_show(struct seq_file
*s
, void *data
)
2669 struct generic_pm_domain
*genpd
;
2672 seq_puts(s
, "domain status slaves\n");
2673 seq_puts(s
, " /device runtime status\n");
2674 seq_puts(s
, "----------------------------------------------------------------------\n");
2676 ret
= mutex_lock_interruptible(&gpd_list_lock
);
2678 return -ERESTARTSYS
;
2680 list_for_each_entry(genpd
, &gpd_list
, gpd_list_node
) {
2681 ret
= genpd_summary_one(s
, genpd
);
2685 mutex_unlock(&gpd_list_lock
);
2690 static int genpd_status_show(struct seq_file
*s
, void *data
)
2692 static const char * const status_lookup
[] = {
2693 [GPD_STATE_ACTIVE
] = "on",
2694 [GPD_STATE_POWER_OFF
] = "off"
2697 struct generic_pm_domain
*genpd
= s
->private;
2700 ret
= genpd_lock_interruptible(genpd
);
2702 return -ERESTARTSYS
;
2704 if (WARN_ON_ONCE(genpd
->status
>= ARRAY_SIZE(status_lookup
)))
2707 if (genpd
->status
== GPD_STATE_POWER_OFF
)
2708 seq_printf(s
, "%s-%u\n", status_lookup
[genpd
->status
],
2711 seq_printf(s
, "%s\n", status_lookup
[genpd
->status
]);
2713 genpd_unlock(genpd
);
2717 static int genpd_sub_domains_show(struct seq_file
*s
, void *data
)
2719 struct generic_pm_domain
*genpd
= s
->private;
2720 struct gpd_link
*link
;
2723 ret
= genpd_lock_interruptible(genpd
);
2725 return -ERESTARTSYS
;
2727 list_for_each_entry(link
, &genpd
->master_links
, master_node
)
2728 seq_printf(s
, "%s\n", link
->slave
->name
);
2730 genpd_unlock(genpd
);
2734 static int genpd_idle_states_show(struct seq_file
*s
, void *data
)
2736 struct generic_pm_domain
*genpd
= s
->private;
2740 ret
= genpd_lock_interruptible(genpd
);
2742 return -ERESTARTSYS
;
2744 seq_puts(s
, "State Time Spent(ms)\n");
2746 for (i
= 0; i
< genpd
->state_count
; i
++) {
2750 if ((genpd
->status
== GPD_STATE_POWER_OFF
) &&
2751 (genpd
->state_idx
== i
))
2752 delta
= ktime_sub(ktime_get(), genpd
->accounting_time
);
2754 msecs
= ktime_to_ms(
2755 ktime_add(genpd
->states
[i
].idle_time
, delta
));
2756 seq_printf(s
, "S%-13i %lld\n", i
, msecs
);
2759 genpd_unlock(genpd
);
2763 static int genpd_active_time_show(struct seq_file
*s
, void *data
)
2765 struct generic_pm_domain
*genpd
= s
->private;
2769 ret
= genpd_lock_interruptible(genpd
);
2771 return -ERESTARTSYS
;
2773 if (genpd
->status
== GPD_STATE_ACTIVE
)
2774 delta
= ktime_sub(ktime_get(), genpd
->accounting_time
);
2776 seq_printf(s
, "%lld ms\n", ktime_to_ms(
2777 ktime_add(genpd
->on_time
, delta
)));
2779 genpd_unlock(genpd
);
2783 static int genpd_total_idle_time_show(struct seq_file
*s
, void *data
)
2785 struct generic_pm_domain
*genpd
= s
->private;
2786 ktime_t delta
= 0, total
= 0;
2790 ret
= genpd_lock_interruptible(genpd
);
2792 return -ERESTARTSYS
;
2794 for (i
= 0; i
< genpd
->state_count
; i
++) {
2796 if ((genpd
->status
== GPD_STATE_POWER_OFF
) &&
2797 (genpd
->state_idx
== i
))
2798 delta
= ktime_sub(ktime_get(), genpd
->accounting_time
);
2800 total
= ktime_add(total
, genpd
->states
[i
].idle_time
);
2802 total
= ktime_add(total
, delta
);
2804 seq_printf(s
, "%lld ms\n", ktime_to_ms(total
));
2806 genpd_unlock(genpd
);
2811 static int genpd_devices_show(struct seq_file
*s
, void *data
)
2813 struct generic_pm_domain
*genpd
= s
->private;
2814 struct pm_domain_data
*pm_data
;
2815 const char *kobj_path
;
2818 ret
= genpd_lock_interruptible(genpd
);
2820 return -ERESTARTSYS
;
2822 list_for_each_entry(pm_data
, &genpd
->dev_list
, list_node
) {
2823 kobj_path
= kobject_get_path(&pm_data
->dev
->kobj
,
2824 genpd_is_irq_safe(genpd
) ?
2825 GFP_ATOMIC
: GFP_KERNEL
);
2826 if (kobj_path
== NULL
)
2829 seq_printf(s
, "%s\n", kobj_path
);
2833 genpd_unlock(genpd
);
2837 static int genpd_perf_state_show(struct seq_file
*s
, void *data
)
2839 struct generic_pm_domain
*genpd
= s
->private;
2841 if (genpd_lock_interruptible(genpd
))
2842 return -ERESTARTSYS
;
2844 seq_printf(s
, "%u\n", genpd
->performance_state
);
2846 genpd_unlock(genpd
);
2850 #define define_genpd_open_function(name) \
2851 static int genpd_##name##_open(struct inode *inode, struct file *file) \
2853 return single_open(file, genpd_##name##_show, inode->i_private); \
2856 define_genpd_open_function(summary
);
2857 define_genpd_open_function(status
);
2858 define_genpd_open_function(sub_domains
);
2859 define_genpd_open_function(idle_states
);
2860 define_genpd_open_function(active_time
);
2861 define_genpd_open_function(total_idle_time
);
2862 define_genpd_open_function(devices
);
2863 define_genpd_open_function(perf_state
);
2865 #define define_genpd_debugfs_fops(name) \
2866 static const struct file_operations genpd_##name##_fops = { \
2867 .open = genpd_##name##_open, \
2869 .llseek = seq_lseek, \
2870 .release = single_release, \
2873 define_genpd_debugfs_fops(summary
);
2874 define_genpd_debugfs_fops(status
);
2875 define_genpd_debugfs_fops(sub_domains
);
2876 define_genpd_debugfs_fops(idle_states
);
2877 define_genpd_debugfs_fops(active_time
);
2878 define_genpd_debugfs_fops(total_idle_time
);
2879 define_genpd_debugfs_fops(devices
);
2880 define_genpd_debugfs_fops(perf_state
);
2882 static int __init
genpd_debug_init(void)
2885 struct generic_pm_domain
*genpd
;
2887 genpd_debugfs_dir
= debugfs_create_dir("pm_genpd", NULL
);
2889 if (!genpd_debugfs_dir
)
2892 d
= debugfs_create_file("pm_genpd_summary", S_IRUGO
,
2893 genpd_debugfs_dir
, NULL
, &genpd_summary_fops
);
2897 list_for_each_entry(genpd
, &gpd_list
, gpd_list_node
) {
2898 d
= debugfs_create_dir(genpd
->name
, genpd_debugfs_dir
);
2902 debugfs_create_file("current_state", 0444,
2903 d
, genpd
, &genpd_status_fops
);
2904 debugfs_create_file("sub_domains", 0444,
2905 d
, genpd
, &genpd_sub_domains_fops
);
2906 debugfs_create_file("idle_states", 0444,
2907 d
, genpd
, &genpd_idle_states_fops
);
2908 debugfs_create_file("active_time", 0444,
2909 d
, genpd
, &genpd_active_time_fops
);
2910 debugfs_create_file("total_idle_time", 0444,
2911 d
, genpd
, &genpd_total_idle_time_fops
);
2912 debugfs_create_file("devices", 0444,
2913 d
, genpd
, &genpd_devices_fops
);
2914 if (genpd
->set_performance_state
)
2915 debugfs_create_file("perf_state", 0444,
2916 d
, genpd
, &genpd_perf_state_fops
);
2921 late_initcall(genpd_debug_init
);
2923 static void __exit
genpd_debug_exit(void)
2925 debugfs_remove_recursive(genpd_debugfs_dir
);
2927 __exitcall(genpd_debug_exit
);
2928 #endif /* CONFIG_DEBUG_FS */