1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/power/domain.c - Common code related to device power domains.
5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
7 #define pr_fmt(fmt) "PM: " fmt
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>
23 #include <linux/cpu.h>
27 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
29 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
31 type (*__routine)(struct device *__d); \
32 type __ret = (type)0; \
34 __routine = genpd->dev_ops.callback; \
36 __ret = __routine(dev); \
41 static LIST_HEAD(gpd_list
);
42 static DEFINE_MUTEX(gpd_list_lock
);
44 struct genpd_lock_ops
{
45 void (*lock
)(struct generic_pm_domain
*genpd
);
46 void (*lock_nested
)(struct generic_pm_domain
*genpd
, int depth
);
47 int (*lock_interruptible
)(struct generic_pm_domain
*genpd
);
48 void (*unlock
)(struct generic_pm_domain
*genpd
);
51 static void genpd_lock_mtx(struct generic_pm_domain
*genpd
)
53 mutex_lock(&genpd
->mlock
);
56 static void genpd_lock_nested_mtx(struct generic_pm_domain
*genpd
,
59 mutex_lock_nested(&genpd
->mlock
, depth
);
62 static int genpd_lock_interruptible_mtx(struct generic_pm_domain
*genpd
)
64 return mutex_lock_interruptible(&genpd
->mlock
);
67 static void genpd_unlock_mtx(struct generic_pm_domain
*genpd
)
69 return mutex_unlock(&genpd
->mlock
);
72 static const struct genpd_lock_ops genpd_mtx_ops
= {
73 .lock
= genpd_lock_mtx
,
74 .lock_nested
= genpd_lock_nested_mtx
,
75 .lock_interruptible
= genpd_lock_interruptible_mtx
,
76 .unlock
= genpd_unlock_mtx
,
79 static void genpd_lock_spin(struct generic_pm_domain
*genpd
)
80 __acquires(&genpd
->slock
)
84 spin_lock_irqsave(&genpd
->slock
, flags
);
85 genpd
->lock_flags
= flags
;
88 static void genpd_lock_nested_spin(struct generic_pm_domain
*genpd
,
90 __acquires(&genpd
->slock
)
94 spin_lock_irqsave_nested(&genpd
->slock
, flags
, depth
);
95 genpd
->lock_flags
= flags
;
98 static int genpd_lock_interruptible_spin(struct generic_pm_domain
*genpd
)
99 __acquires(&genpd
->slock
)
103 spin_lock_irqsave(&genpd
->slock
, flags
);
104 genpd
->lock_flags
= flags
;
108 static void genpd_unlock_spin(struct generic_pm_domain
*genpd
)
109 __releases(&genpd
->slock
)
111 spin_unlock_irqrestore(&genpd
->slock
, genpd
->lock_flags
);
114 static const struct genpd_lock_ops genpd_spin_ops
= {
115 .lock
= genpd_lock_spin
,
116 .lock_nested
= genpd_lock_nested_spin
,
117 .lock_interruptible
= genpd_lock_interruptible_spin
,
118 .unlock
= genpd_unlock_spin
,
121 #define genpd_lock(p) p->lock_ops->lock(p)
122 #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
123 #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
124 #define genpd_unlock(p) p->lock_ops->unlock(p)
126 #define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
127 #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
128 #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
129 #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
130 #define genpd_is_cpu_domain(genpd) (genpd->flags & GENPD_FLAG_CPU_DOMAIN)
131 #define genpd_is_rpm_always_on(genpd) (genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
133 static inline bool irq_safe_dev_in_no_sleep_domain(struct device
*dev
,
134 const struct generic_pm_domain
*genpd
)
138 ret
= pm_runtime_is_irq_safe(dev
) && !genpd_is_irq_safe(genpd
);
141 * Warn once if an IRQ safe device is attached to a no sleep domain, as
142 * to indicate a suboptimal configuration for PM. For an always on
143 * domain this isn't case, thus don't warn.
145 if (ret
&& !genpd_is_always_on(genpd
))
146 dev_warn_once(dev
, "PM domain %s will not be powered off\n",
152 static int genpd_runtime_suspend(struct device
*dev
);
155 * Get the generic PM domain for a particular struct device.
156 * This validates the struct device pointer, the PM domain pointer,
157 * and checks that the PM domain pointer is a real generic PM domain.
158 * Any failure results in NULL being returned.
160 static struct generic_pm_domain
*dev_to_genpd_safe(struct device
*dev
)
162 if (IS_ERR_OR_NULL(dev
) || IS_ERR_OR_NULL(dev
->pm_domain
))
165 /* A genpd's always have its ->runtime_suspend() callback assigned. */
166 if (dev
->pm_domain
->ops
.runtime_suspend
== genpd_runtime_suspend
)
167 return pd_to_genpd(dev
->pm_domain
);
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 #ifdef CONFIG_DEBUG_FS
213 static void genpd_update_accounting(struct generic_pm_domain
*genpd
)
218 delta
= ktime_sub(now
, genpd
->accounting_time
);
221 * If genpd->status is active, it means we are just
222 * out of off and so update the idle time and vice
225 if (genpd
->status
== GPD_STATE_ACTIVE
) {
226 int state_idx
= genpd
->state_idx
;
228 genpd
->states
[state_idx
].idle_time
=
229 ktime_add(genpd
->states
[state_idx
].idle_time
, delta
);
231 genpd
->on_time
= ktime_add(genpd
->on_time
, delta
);
234 genpd
->accounting_time
= now
;
237 static inline void genpd_update_accounting(struct generic_pm_domain
*genpd
) {}
240 static int _genpd_reeval_performance_state(struct generic_pm_domain
*genpd
,
243 struct generic_pm_domain_data
*pd_data
;
244 struct pm_domain_data
*pdd
;
245 struct gpd_link
*link
;
247 /* New requested state is same as Max requested state */
248 if (state
== genpd
->performance_state
)
251 /* New requested state is higher than Max requested state */
252 if (state
> genpd
->performance_state
)
255 /* Traverse all devices within the domain */
256 list_for_each_entry(pdd
, &genpd
->dev_list
, list_node
) {
257 pd_data
= to_gpd_data(pdd
);
259 if (pd_data
->performance_state
> state
)
260 state
= pd_data
->performance_state
;
264 * Traverse all sub-domains within the domain. This can be
265 * done without any additional locking as the link->performance_state
266 * field is protected by the master genpd->lock, which is already taken.
268 * Also note that link->performance_state (subdomain's performance state
269 * requirement to master domain) is different from
270 * link->slave->performance_state (current performance state requirement
271 * of the devices/sub-domains of the subdomain) and so can have a
274 * Note that we also take vote from powered-off sub-domains into account
275 * as the same is done for devices right now.
277 list_for_each_entry(link
, &genpd
->master_links
, master_node
) {
278 if (link
->performance_state
> state
)
279 state
= link
->performance_state
;
285 static int _genpd_set_performance_state(struct generic_pm_domain
*genpd
,
286 unsigned int state
, int depth
)
288 struct generic_pm_domain
*master
;
289 struct gpd_link
*link
;
290 int master_state
, ret
;
292 if (state
== genpd
->performance_state
)
295 /* Propagate to masters of genpd */
296 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
297 master
= link
->master
;
299 if (!master
->set_performance_state
)
302 /* Find master's performance state */
303 ret
= dev_pm_opp_xlate_performance_state(genpd
->opp_table
,
306 if (unlikely(ret
< 0))
311 genpd_lock_nested(master
, depth
+ 1);
313 link
->prev_performance_state
= link
->performance_state
;
314 link
->performance_state
= master_state
;
315 master_state
= _genpd_reeval_performance_state(master
,
317 ret
= _genpd_set_performance_state(master
, master_state
, depth
+ 1);
319 link
->performance_state
= link
->prev_performance_state
;
321 genpd_unlock(master
);
327 ret
= genpd
->set_performance_state(genpd
, state
);
331 genpd
->performance_state
= state
;
335 /* Encountered an error, lets rollback */
336 list_for_each_entry_continue_reverse(link
, &genpd
->slave_links
,
338 master
= link
->master
;
340 if (!master
->set_performance_state
)
343 genpd_lock_nested(master
, depth
+ 1);
345 master_state
= link
->prev_performance_state
;
346 link
->performance_state
= master_state
;
348 master_state
= _genpd_reeval_performance_state(master
,
350 if (_genpd_set_performance_state(master
, master_state
, depth
+ 1)) {
351 pr_err("%s: Failed to roll back to %d performance state\n",
352 master
->name
, master_state
);
355 genpd_unlock(master
);
362 * dev_pm_genpd_set_performance_state- Set performance state of device's power
365 * @dev: Device for which the performance-state needs to be set.
366 * @state: Target performance state of the device. This can be set as 0 when the
367 * device doesn't have any performance state constraints left (And so
368 * the device wouldn't participate anymore to find the target
369 * performance state of the genpd).
371 * It is assumed that the users guarantee that the genpd wouldn't be detached
372 * while this routine is getting called.
374 * Returns 0 on success and negative error values on failures.
376 int dev_pm_genpd_set_performance_state(struct device
*dev
, unsigned int state
)
378 struct generic_pm_domain
*genpd
;
379 struct generic_pm_domain_data
*gpd_data
;
383 genpd
= dev_to_genpd_safe(dev
);
387 if (unlikely(!genpd
->set_performance_state
))
390 if (WARN_ON(!dev
->power
.subsys_data
||
391 !dev
->power
.subsys_data
->domain_data
))
396 gpd_data
= to_gpd_data(dev
->power
.subsys_data
->domain_data
);
397 prev
= gpd_data
->performance_state
;
398 gpd_data
->performance_state
= state
;
400 state
= _genpd_reeval_performance_state(genpd
, state
);
401 ret
= _genpd_set_performance_state(genpd
, state
, 0);
403 gpd_data
->performance_state
= prev
;
409 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state
);
411 static int _genpd_power_on(struct generic_pm_domain
*genpd
, bool timed
)
413 unsigned int state_idx
= genpd
->state_idx
;
418 if (!genpd
->power_on
)
422 return genpd
->power_on(genpd
);
424 time_start
= ktime_get();
425 ret
= genpd
->power_on(genpd
);
429 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
430 if (elapsed_ns
<= genpd
->states
[state_idx
].power_on_latency_ns
)
433 genpd
->states
[state_idx
].power_on_latency_ns
= elapsed_ns
;
434 genpd
->max_off_time_changed
= true;
435 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
436 genpd
->name
, "on", elapsed_ns
);
441 static int _genpd_power_off(struct generic_pm_domain
*genpd
, bool timed
)
443 unsigned int state_idx
= genpd
->state_idx
;
448 if (!genpd
->power_off
)
452 return genpd
->power_off(genpd
);
454 time_start
= ktime_get();
455 ret
= genpd
->power_off(genpd
);
459 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
460 if (elapsed_ns
<= genpd
->states
[state_idx
].power_off_latency_ns
)
463 genpd
->states
[state_idx
].power_off_latency_ns
= elapsed_ns
;
464 genpd
->max_off_time_changed
= true;
465 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
466 genpd
->name
, "off", elapsed_ns
);
472 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
473 * @genpd: PM domain to power off.
475 * Queue up the execution of genpd_power_off() unless it's already been done
478 static void genpd_queue_power_off_work(struct generic_pm_domain
*genpd
)
480 queue_work(pm_wq
, &genpd
->power_off_work
);
484 * genpd_power_off - Remove power from a given PM domain.
485 * @genpd: PM domain to power down.
486 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
487 * RPM status of the releated device is in an intermediate state, not yet turned
488 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
489 * be RPM_SUSPENDED, while it tries to power off the PM domain.
491 * If all of the @genpd's devices have been suspended and all of its subdomains
492 * have been powered down, remove power from @genpd.
494 static int genpd_power_off(struct generic_pm_domain
*genpd
, bool one_dev_on
,
497 struct pm_domain_data
*pdd
;
498 struct gpd_link
*link
;
499 unsigned int not_suspended
= 0;
502 * Do not try to power off the domain in the following situations:
503 * (1) The domain is already in the "power off" state.
504 * (2) System suspend is in progress.
506 if (!genpd_status_on(genpd
) || genpd
->prepared_count
> 0)
510 * Abort power off for the PM domain in the following situations:
511 * (1) The domain is configured as always on.
512 * (2) When the domain has a subdomain being powered on.
514 if (genpd_is_always_on(genpd
) ||
515 genpd_is_rpm_always_on(genpd
) ||
516 atomic_read(&genpd
->sd_count
) > 0)
519 list_for_each_entry(pdd
, &genpd
->dev_list
, list_node
) {
520 enum pm_qos_flags_status stat
;
522 stat
= dev_pm_qos_flags(pdd
->dev
, PM_QOS_FLAG_NO_POWER_OFF
);
523 if (stat
> PM_QOS_FLAGS_NONE
)
527 * Do not allow PM domain to be powered off, when an IRQ safe
528 * device is part of a non-IRQ safe domain.
530 if (!pm_runtime_suspended(pdd
->dev
) ||
531 irq_safe_dev_in_no_sleep_domain(pdd
->dev
, genpd
))
535 if (not_suspended
> 1 || (not_suspended
== 1 && !one_dev_on
))
538 if (genpd
->gov
&& genpd
->gov
->power_down_ok
) {
539 if (!genpd
->gov
->power_down_ok(&genpd
->domain
))
543 /* Default to shallowest state. */
545 genpd
->state_idx
= 0;
547 if (genpd
->power_off
) {
550 if (atomic_read(&genpd
->sd_count
) > 0)
554 * If sd_count > 0 at this point, one of the subdomains hasn't
555 * managed to call genpd_power_on() for the master yet after
556 * incrementing it. In that case genpd_power_on() will wait
557 * for us to drop the lock, so we can call .power_off() and let
558 * the genpd_power_on() restore power for us (this shouldn't
559 * happen very often).
561 ret
= _genpd_power_off(genpd
, true);
566 genpd
->status
= GPD_STATE_POWER_OFF
;
567 genpd_update_accounting(genpd
);
569 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
570 genpd_sd_counter_dec(link
->master
);
571 genpd_lock_nested(link
->master
, depth
+ 1);
572 genpd_power_off(link
->master
, false, depth
+ 1);
573 genpd_unlock(link
->master
);
580 * genpd_power_on - Restore power to a given PM domain and its masters.
581 * @genpd: PM domain to power up.
582 * @depth: nesting count for lockdep.
584 * Restore power to @genpd and all of its masters so that it is possible to
585 * resume a device belonging to it.
587 static int genpd_power_on(struct generic_pm_domain
*genpd
, unsigned int depth
)
589 struct gpd_link
*link
;
592 if (genpd_status_on(genpd
))
596 * The list is guaranteed not to change while the loop below is being
597 * executed, unless one of the masters' .power_on() callbacks fiddles
600 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
601 struct generic_pm_domain
*master
= link
->master
;
603 genpd_sd_counter_inc(master
);
605 genpd_lock_nested(master
, depth
+ 1);
606 ret
= genpd_power_on(master
, depth
+ 1);
607 genpd_unlock(master
);
610 genpd_sd_counter_dec(master
);
615 ret
= _genpd_power_on(genpd
, true);
619 genpd
->status
= GPD_STATE_ACTIVE
;
620 genpd_update_accounting(genpd
);
625 list_for_each_entry_continue_reverse(link
,
628 genpd_sd_counter_dec(link
->master
);
629 genpd_lock_nested(link
->master
, depth
+ 1);
630 genpd_power_off(link
->master
, false, depth
+ 1);
631 genpd_unlock(link
->master
);
637 static int genpd_dev_pm_start(struct device
*dev
)
639 struct generic_pm_domain
*genpd
= dev_to_genpd(dev
);
641 return genpd_start_dev(genpd
, dev
);
644 static int genpd_dev_pm_qos_notifier(struct notifier_block
*nb
,
645 unsigned long val
, void *ptr
)
647 struct generic_pm_domain_data
*gpd_data
;
650 gpd_data
= container_of(nb
, struct generic_pm_domain_data
, nb
);
651 dev
= gpd_data
->base
.dev
;
654 struct generic_pm_domain
*genpd
;
655 struct pm_domain_data
*pdd
;
657 spin_lock_irq(&dev
->power
.lock
);
659 pdd
= dev
->power
.subsys_data
?
660 dev
->power
.subsys_data
->domain_data
: NULL
;
662 to_gpd_data(pdd
)->td
.constraint_changed
= true;
663 genpd
= dev_to_genpd(dev
);
665 genpd
= ERR_PTR(-ENODATA
);
668 spin_unlock_irq(&dev
->power
.lock
);
670 if (!IS_ERR(genpd
)) {
672 genpd
->max_off_time_changed
= true;
677 if (!dev
|| dev
->power
.ignore_children
)
685 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
686 * @work: Work structure used for scheduling the execution of this function.
688 static void genpd_power_off_work_fn(struct work_struct
*work
)
690 struct generic_pm_domain
*genpd
;
692 genpd
= container_of(work
, struct generic_pm_domain
, power_off_work
);
695 genpd_power_off(genpd
, false, 0);
700 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
701 * @dev: Device to handle.
703 static int __genpd_runtime_suspend(struct device
*dev
)
705 int (*cb
)(struct device
*__dev
);
707 if (dev
->type
&& dev
->type
->pm
)
708 cb
= dev
->type
->pm
->runtime_suspend
;
709 else if (dev
->class && dev
->class->pm
)
710 cb
= dev
->class->pm
->runtime_suspend
;
711 else if (dev
->bus
&& dev
->bus
->pm
)
712 cb
= dev
->bus
->pm
->runtime_suspend
;
716 if (!cb
&& dev
->driver
&& dev
->driver
->pm
)
717 cb
= dev
->driver
->pm
->runtime_suspend
;
719 return cb
? cb(dev
) : 0;
723 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
724 * @dev: Device to handle.
726 static int __genpd_runtime_resume(struct device
*dev
)
728 int (*cb
)(struct device
*__dev
);
730 if (dev
->type
&& dev
->type
->pm
)
731 cb
= dev
->type
->pm
->runtime_resume
;
732 else if (dev
->class && dev
->class->pm
)
733 cb
= dev
->class->pm
->runtime_resume
;
734 else if (dev
->bus
&& dev
->bus
->pm
)
735 cb
= dev
->bus
->pm
->runtime_resume
;
739 if (!cb
&& dev
->driver
&& dev
->driver
->pm
)
740 cb
= dev
->driver
->pm
->runtime_resume
;
742 return cb
? cb(dev
) : 0;
746 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
747 * @dev: Device to suspend.
749 * Carry out a runtime suspend of a device under the assumption that its
750 * pm_domain field points to the domain member of an object of type
751 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
753 static int genpd_runtime_suspend(struct device
*dev
)
755 struct generic_pm_domain
*genpd
;
756 bool (*suspend_ok
)(struct device
*__dev
);
757 struct gpd_timing_data
*td
= &dev_gpd_data(dev
)->td
;
758 bool runtime_pm
= pm_runtime_enabled(dev
);
763 dev_dbg(dev
, "%s()\n", __func__
);
765 genpd
= dev_to_genpd(dev
);
770 * A runtime PM centric subsystem/driver may re-use the runtime PM
771 * callbacks for other purposes than runtime PM. In those scenarios
772 * runtime PM is disabled. Under these circumstances, we shall skip
773 * validating/measuring the PM QoS latency.
775 suspend_ok
= genpd
->gov
? genpd
->gov
->suspend_ok
: NULL
;
776 if (runtime_pm
&& suspend_ok
&& !suspend_ok(dev
))
779 /* Measure suspend latency. */
782 time_start
= ktime_get();
784 ret
= __genpd_runtime_suspend(dev
);
788 ret
= genpd_stop_dev(genpd
, dev
);
790 __genpd_runtime_resume(dev
);
794 /* Update suspend latency value if the measured time exceeds it. */
796 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
797 if (elapsed_ns
> td
->suspend_latency_ns
) {
798 td
->suspend_latency_ns
= elapsed_ns
;
799 dev_dbg(dev
, "suspend latency exceeded, %lld ns\n",
801 genpd
->max_off_time_changed
= true;
802 td
->constraint_changed
= true;
807 * If power.irq_safe is set, this routine may be run with
808 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
810 if (irq_safe_dev_in_no_sleep_domain(dev
, genpd
))
814 genpd_power_off(genpd
, true, 0);
821 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
822 * @dev: Device to resume.
824 * Carry out a runtime resume of a device under the assumption that its
825 * pm_domain field points to the domain member of an object of type
826 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
828 static int genpd_runtime_resume(struct device
*dev
)
830 struct generic_pm_domain
*genpd
;
831 struct gpd_timing_data
*td
= &dev_gpd_data(dev
)->td
;
832 bool runtime_pm
= pm_runtime_enabled(dev
);
838 dev_dbg(dev
, "%s()\n", __func__
);
840 genpd
= dev_to_genpd(dev
);
845 * As we don't power off a non IRQ safe domain, which holds
846 * an IRQ safe device, we don't need to restore power to it.
848 if (irq_safe_dev_in_no_sleep_domain(dev
, genpd
)) {
854 ret
= genpd_power_on(genpd
, 0);
861 /* Measure resume latency. */
863 if (timed
&& runtime_pm
)
864 time_start
= ktime_get();
866 ret
= genpd_start_dev(genpd
, dev
);
870 ret
= __genpd_runtime_resume(dev
);
874 /* Update resume latency value if the measured time exceeds it. */
875 if (timed
&& runtime_pm
) {
876 elapsed_ns
= ktime_to_ns(ktime_sub(ktime_get(), time_start
));
877 if (elapsed_ns
> td
->resume_latency_ns
) {
878 td
->resume_latency_ns
= elapsed_ns
;
879 dev_dbg(dev
, "resume latency exceeded, %lld ns\n",
881 genpd
->max_off_time_changed
= true;
882 td
->constraint_changed
= true;
889 genpd_stop_dev(genpd
, dev
);
891 if (!pm_runtime_is_irq_safe(dev
) ||
892 (pm_runtime_is_irq_safe(dev
) && genpd_is_irq_safe(genpd
))) {
894 genpd_power_off(genpd
, true, 0);
901 static bool pd_ignore_unused
;
902 static int __init
pd_ignore_unused_setup(char *__unused
)
904 pd_ignore_unused
= true;
907 __setup("pd_ignore_unused", pd_ignore_unused_setup
);
910 * genpd_power_off_unused - Power off all PM domains with no devices in use.
912 static int __init
genpd_power_off_unused(void)
914 struct generic_pm_domain
*genpd
;
916 if (pd_ignore_unused
) {
917 pr_warn("genpd: Not disabling unused power domains\n");
921 mutex_lock(&gpd_list_lock
);
923 list_for_each_entry(genpd
, &gpd_list
, gpd_list_node
)
924 genpd_queue_power_off_work(genpd
);
926 mutex_unlock(&gpd_list_lock
);
930 late_initcall(genpd_power_off_unused
);
932 #ifdef CONFIG_PM_SLEEP
935 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
936 * @genpd: PM domain to power off, if possible.
937 * @use_lock: use the lock.
938 * @depth: nesting count for lockdep.
940 * Check if the given PM domain can be powered off (during system suspend or
941 * hibernation) and do that if so. Also, in that case propagate to its masters.
943 * This function is only called in "noirq" and "syscore" stages of system power
944 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
945 * these cases the lock must be held.
947 static void genpd_sync_power_off(struct generic_pm_domain
*genpd
, bool use_lock
,
950 struct gpd_link
*link
;
952 if (!genpd_status_on(genpd
) || genpd_is_always_on(genpd
))
955 if (genpd
->suspended_count
!= genpd
->device_count
956 || atomic_read(&genpd
->sd_count
) > 0)
959 /* Choose the deepest state when suspending */
960 genpd
->state_idx
= genpd
->state_count
- 1;
961 if (_genpd_power_off(genpd
, false))
964 genpd
->status
= GPD_STATE_POWER_OFF
;
966 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
967 genpd_sd_counter_dec(link
->master
);
970 genpd_lock_nested(link
->master
, depth
+ 1);
972 genpd_sync_power_off(link
->master
, use_lock
, depth
+ 1);
975 genpd_unlock(link
->master
);
980 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
981 * @genpd: PM domain to power on.
982 * @use_lock: use the lock.
983 * @depth: nesting count for lockdep.
985 * This function is only called in "noirq" and "syscore" stages of system power
986 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
987 * these cases the lock must be held.
989 static void genpd_sync_power_on(struct generic_pm_domain
*genpd
, bool use_lock
,
992 struct gpd_link
*link
;
994 if (genpd_status_on(genpd
))
997 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
998 genpd_sd_counter_inc(link
->master
);
1001 genpd_lock_nested(link
->master
, depth
+ 1);
1003 genpd_sync_power_on(link
->master
, use_lock
, depth
+ 1);
1006 genpd_unlock(link
->master
);
1009 _genpd_power_on(genpd
, false);
1011 genpd
->status
= GPD_STATE_ACTIVE
;
1015 * resume_needed - Check whether to resume a device before system suspend.
1016 * @dev: Device to check.
1017 * @genpd: PM domain the device belongs to.
1019 * There are two cases in which a device that can wake up the system from sleep
1020 * states should be resumed by genpd_prepare(): (1) if the device is enabled
1021 * to wake up the system and it has to remain active for this purpose while the
1022 * system is in the sleep state and (2) if the device is not enabled to wake up
1023 * the system from sleep states and it generally doesn't generate wakeup signals
1024 * by itself (those signals are generated on its behalf by other parts of the
1025 * system). In the latter case it may be necessary to reconfigure the device's
1026 * wakeup settings during system suspend, because it may have been set up to
1027 * signal remote wakeup from the system's working state as needed by runtime PM.
1028 * Return 'true' in either of the above cases.
1030 static bool resume_needed(struct device
*dev
,
1031 const struct generic_pm_domain
*genpd
)
1035 if (!device_can_wakeup(dev
))
1038 active_wakeup
= genpd_is_active_wakeup(genpd
);
1039 return device_may_wakeup(dev
) ? active_wakeup
: !active_wakeup
;
1043 * genpd_prepare - Start power transition of a device in a PM domain.
1044 * @dev: Device to start the transition of.
1046 * Start a power transition of a device (during a system-wide power transition)
1047 * under the assumption that its pm_domain field points to the domain member of
1048 * an object of type struct generic_pm_domain representing a PM domain
1049 * consisting of I/O devices.
1051 static int genpd_prepare(struct device
*dev
)
1053 struct generic_pm_domain
*genpd
;
1056 dev_dbg(dev
, "%s()\n", __func__
);
1058 genpd
= dev_to_genpd(dev
);
1063 * If a wakeup request is pending for the device, it should be woken up
1064 * at this point and a system wakeup event should be reported if it's
1065 * set up to wake up the system from sleep states.
1067 if (resume_needed(dev
, genpd
))
1068 pm_runtime_resume(dev
);
1072 if (genpd
->prepared_count
++ == 0)
1073 genpd
->suspended_count
= 0;
1075 genpd_unlock(genpd
);
1077 ret
= pm_generic_prepare(dev
);
1081 genpd
->prepared_count
--;
1083 genpd_unlock(genpd
);
1086 /* Never return 1, as genpd don't cope with the direct_complete path. */
1087 return ret
>= 0 ? 0 : ret
;
1091 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1093 * @dev: Device to suspend.
1094 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
1096 * Stop the device and remove power from the domain if all devices in it have
1099 static int genpd_finish_suspend(struct device
*dev
, bool poweroff
)
1101 struct generic_pm_domain
*genpd
;
1104 genpd
= dev_to_genpd(dev
);
1109 ret
= pm_generic_poweroff_noirq(dev
);
1111 ret
= pm_generic_suspend_noirq(dev
);
1115 if (dev
->power
.wakeup_path
&& genpd_is_active_wakeup(genpd
))
1118 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
&&
1119 !pm_runtime_status_suspended(dev
)) {
1120 ret
= genpd_stop_dev(genpd
, dev
);
1123 pm_generic_restore_noirq(dev
);
1125 pm_generic_resume_noirq(dev
);
1131 genpd
->suspended_count
++;
1132 genpd_sync_power_off(genpd
, true, 0);
1133 genpd_unlock(genpd
);
1139 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1140 * @dev: Device to suspend.
1142 * Stop the device and remove power from the domain if all devices in it have
1145 static int genpd_suspend_noirq(struct device
*dev
)
1147 dev_dbg(dev
, "%s()\n", __func__
);
1149 return genpd_finish_suspend(dev
, false);
1153 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1154 * @dev: Device to resume.
1156 * Restore power to the device's PM domain, if necessary, and start the device.
1158 static int genpd_resume_noirq(struct device
*dev
)
1160 struct generic_pm_domain
*genpd
;
1163 dev_dbg(dev
, "%s()\n", __func__
);
1165 genpd
= dev_to_genpd(dev
);
1169 if (dev
->power
.wakeup_path
&& genpd_is_active_wakeup(genpd
))
1170 return pm_generic_resume_noirq(dev
);
1173 genpd_sync_power_on(genpd
, true, 0);
1174 genpd
->suspended_count
--;
1175 genpd_unlock(genpd
);
1177 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
&&
1178 !pm_runtime_status_suspended(dev
)) {
1179 ret
= genpd_start_dev(genpd
, dev
);
1184 return pm_generic_resume_noirq(dev
);
1188 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1189 * @dev: Device to freeze.
1191 * Carry out a late freeze of a device under the assumption that its
1192 * pm_domain field points to the domain member of an object of type
1193 * struct generic_pm_domain representing a power domain consisting of I/O
1196 static int genpd_freeze_noirq(struct device
*dev
)
1198 const struct generic_pm_domain
*genpd
;
1201 dev_dbg(dev
, "%s()\n", __func__
);
1203 genpd
= dev_to_genpd(dev
);
1207 ret
= pm_generic_freeze_noirq(dev
);
1211 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
&&
1212 !pm_runtime_status_suspended(dev
))
1213 ret
= genpd_stop_dev(genpd
, dev
);
1219 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1220 * @dev: Device to thaw.
1222 * Start the device, unless power has been removed from the domain already
1223 * before the system transition.
1225 static int genpd_thaw_noirq(struct device
*dev
)
1227 const struct generic_pm_domain
*genpd
;
1230 dev_dbg(dev
, "%s()\n", __func__
);
1232 genpd
= dev_to_genpd(dev
);
1236 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
&&
1237 !pm_runtime_status_suspended(dev
)) {
1238 ret
= genpd_start_dev(genpd
, dev
);
1243 return pm_generic_thaw_noirq(dev
);
1247 * genpd_poweroff_noirq - Completion of hibernation of device in an
1249 * @dev: Device to poweroff.
1251 * Stop the device and remove power from the domain if all devices in it have
1254 static int genpd_poweroff_noirq(struct device
*dev
)
1256 dev_dbg(dev
, "%s()\n", __func__
);
1258 return genpd_finish_suspend(dev
, true);
1262 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1263 * @dev: Device to resume.
1265 * Make sure the domain will be in the same power state as before the
1266 * hibernation the system is resuming from and start the device if necessary.
1268 static int genpd_restore_noirq(struct device
*dev
)
1270 struct generic_pm_domain
*genpd
;
1273 dev_dbg(dev
, "%s()\n", __func__
);
1275 genpd
= dev_to_genpd(dev
);
1280 * At this point suspended_count == 0 means we are being run for the
1281 * first time for the given domain in the present cycle.
1284 if (genpd
->suspended_count
++ == 0)
1286 * The boot kernel might put the domain into arbitrary state,
1287 * so make it appear as powered off to genpd_sync_power_on(),
1288 * so that it tries to power it on in case it was really off.
1290 genpd
->status
= GPD_STATE_POWER_OFF
;
1292 genpd_sync_power_on(genpd
, true, 0);
1293 genpd_unlock(genpd
);
1295 if (genpd
->dev_ops
.stop
&& genpd
->dev_ops
.start
&&
1296 !pm_runtime_status_suspended(dev
)) {
1297 ret
= genpd_start_dev(genpd
, dev
);
1302 return pm_generic_restore_noirq(dev
);
1306 * genpd_complete - Complete power transition of a device in a power domain.
1307 * @dev: Device to complete the transition of.
1309 * Complete a power transition of a device (during a system-wide power
1310 * transition) under the assumption that its pm_domain field points to the
1311 * domain member of an object of type struct generic_pm_domain representing
1312 * a power domain consisting of I/O devices.
1314 static void genpd_complete(struct device
*dev
)
1316 struct generic_pm_domain
*genpd
;
1318 dev_dbg(dev
, "%s()\n", __func__
);
1320 genpd
= dev_to_genpd(dev
);
1324 pm_generic_complete(dev
);
1328 genpd
->prepared_count
--;
1329 if (!genpd
->prepared_count
)
1330 genpd_queue_power_off_work(genpd
);
1332 genpd_unlock(genpd
);
1336 * genpd_syscore_switch - Switch power during system core suspend or resume.
1337 * @dev: Device that normally is marked as "always on" to switch power for.
1339 * This routine may only be called during the system core (syscore) suspend or
1340 * resume phase for devices whose "always on" flags are set.
1342 static void genpd_syscore_switch(struct device
*dev
, bool suspend
)
1344 struct generic_pm_domain
*genpd
;
1346 genpd
= dev_to_genpd_safe(dev
);
1351 genpd
->suspended_count
++;
1352 genpd_sync_power_off(genpd
, false, 0);
1354 genpd_sync_power_on(genpd
, false, 0);
1355 genpd
->suspended_count
--;
1359 void pm_genpd_syscore_poweroff(struct device
*dev
)
1361 genpd_syscore_switch(dev
, true);
1363 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff
);
1365 void pm_genpd_syscore_poweron(struct device
*dev
)
1367 genpd_syscore_switch(dev
, false);
1369 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron
);
1371 #else /* !CONFIG_PM_SLEEP */
1373 #define genpd_prepare NULL
1374 #define genpd_suspend_noirq NULL
1375 #define genpd_resume_noirq NULL
1376 #define genpd_freeze_noirq NULL
1377 #define genpd_thaw_noirq NULL
1378 #define genpd_poweroff_noirq NULL
1379 #define genpd_restore_noirq NULL
1380 #define genpd_complete NULL
1382 #endif /* CONFIG_PM_SLEEP */
1384 static struct generic_pm_domain_data
*genpd_alloc_dev_data(struct device
*dev
)
1386 struct generic_pm_domain_data
*gpd_data
;
1389 ret
= dev_pm_get_subsys_data(dev
);
1391 return ERR_PTR(ret
);
1393 gpd_data
= kzalloc(sizeof(*gpd_data
), GFP_KERNEL
);
1399 gpd_data
->base
.dev
= dev
;
1400 gpd_data
->td
.constraint_changed
= true;
1401 gpd_data
->td
.effective_constraint_ns
= PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS
;
1402 gpd_data
->nb
.notifier_call
= genpd_dev_pm_qos_notifier
;
1404 spin_lock_irq(&dev
->power
.lock
);
1406 if (dev
->power
.subsys_data
->domain_data
) {
1411 dev
->power
.subsys_data
->domain_data
= &gpd_data
->base
;
1413 spin_unlock_irq(&dev
->power
.lock
);
1418 spin_unlock_irq(&dev
->power
.lock
);
1421 dev_pm_put_subsys_data(dev
);
1422 return ERR_PTR(ret
);
1425 static void genpd_free_dev_data(struct device
*dev
,
1426 struct generic_pm_domain_data
*gpd_data
)
1428 spin_lock_irq(&dev
->power
.lock
);
1430 dev
->power
.subsys_data
->domain_data
= NULL
;
1432 spin_unlock_irq(&dev
->power
.lock
);
1435 dev_pm_put_subsys_data(dev
);
1438 static void genpd_update_cpumask(struct generic_pm_domain
*genpd
,
1439 int cpu
, bool set
, unsigned int depth
)
1441 struct gpd_link
*link
;
1443 if (!genpd_is_cpu_domain(genpd
))
1446 list_for_each_entry(link
, &genpd
->slave_links
, slave_node
) {
1447 struct generic_pm_domain
*master
= link
->master
;
1449 genpd_lock_nested(master
, depth
+ 1);
1450 genpd_update_cpumask(master
, cpu
, set
, depth
+ 1);
1451 genpd_unlock(master
);
1455 cpumask_set_cpu(cpu
, genpd
->cpus
);
1457 cpumask_clear_cpu(cpu
, genpd
->cpus
);
1460 static void genpd_set_cpumask(struct generic_pm_domain
*genpd
, int cpu
)
1463 genpd_update_cpumask(genpd
, cpu
, true, 0);
1466 static void genpd_clear_cpumask(struct generic_pm_domain
*genpd
, int cpu
)
1469 genpd_update_cpumask(genpd
, cpu
, false, 0);
1472 static int genpd_get_cpu(struct generic_pm_domain
*genpd
, struct device
*dev
)
1476 if (!genpd_is_cpu_domain(genpd
))
1479 for_each_possible_cpu(cpu
) {
1480 if (get_cpu_device(cpu
) == dev
)
1487 static int genpd_add_device(struct generic_pm_domain
*genpd
, struct device
*dev
,
1488 struct device
*base_dev
)
1490 struct generic_pm_domain_data
*gpd_data
;
1493 dev_dbg(dev
, "%s()\n", __func__
);
1495 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(dev
))
1498 gpd_data
= genpd_alloc_dev_data(dev
);
1499 if (IS_ERR(gpd_data
))
1500 return PTR_ERR(gpd_data
);
1502 gpd_data
->cpu
= genpd_get_cpu(genpd
, base_dev
);
1504 ret
= genpd
->attach_dev
? genpd
->attach_dev(genpd
, dev
) : 0;
1510 genpd_set_cpumask(genpd
, gpd_data
->cpu
);
1511 dev_pm_domain_set(dev
, &genpd
->domain
);
1513 genpd
->device_count
++;
1514 genpd
->max_off_time_changed
= true;
1516 list_add_tail(&gpd_data
->base
.list_node
, &genpd
->dev_list
);
1518 genpd_unlock(genpd
);
1521 genpd_free_dev_data(dev
, gpd_data
);
1523 dev_pm_qos_add_notifier(dev
, &gpd_data
->nb
,
1524 DEV_PM_QOS_RESUME_LATENCY
);
1530 * pm_genpd_add_device - Add a device to an I/O PM domain.
1531 * @genpd: PM domain to add the device to.
1532 * @dev: Device to be added.
1534 int pm_genpd_add_device(struct generic_pm_domain
*genpd
, struct device
*dev
)
1538 mutex_lock(&gpd_list_lock
);
1539 ret
= genpd_add_device(genpd
, dev
, dev
);
1540 mutex_unlock(&gpd_list_lock
);
1544 EXPORT_SYMBOL_GPL(pm_genpd_add_device
);
1546 static int genpd_remove_device(struct generic_pm_domain
*genpd
,
1549 struct generic_pm_domain_data
*gpd_data
;
1550 struct pm_domain_data
*pdd
;
1553 dev_dbg(dev
, "%s()\n", __func__
);
1555 pdd
= dev
->power
.subsys_data
->domain_data
;
1556 gpd_data
= to_gpd_data(pdd
);
1557 dev_pm_qos_remove_notifier(dev
, &gpd_data
->nb
,
1558 DEV_PM_QOS_RESUME_LATENCY
);
1562 if (genpd
->prepared_count
> 0) {
1567 genpd
->device_count
--;
1568 genpd
->max_off_time_changed
= true;
1570 genpd_clear_cpumask(genpd
, gpd_data
->cpu
);
1571 dev_pm_domain_set(dev
, NULL
);
1573 list_del_init(&pdd
->list_node
);
1575 genpd_unlock(genpd
);
1577 if (genpd
->detach_dev
)
1578 genpd
->detach_dev(genpd
, dev
);
1580 genpd_free_dev_data(dev
, gpd_data
);
1585 genpd_unlock(genpd
);
1586 dev_pm_qos_add_notifier(dev
, &gpd_data
->nb
, DEV_PM_QOS_RESUME_LATENCY
);
1592 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1593 * @dev: Device to be removed.
1595 int pm_genpd_remove_device(struct device
*dev
)
1597 struct generic_pm_domain
*genpd
= dev_to_genpd_safe(dev
);
1602 return genpd_remove_device(genpd
, dev
);
1604 EXPORT_SYMBOL_GPL(pm_genpd_remove_device
);
1606 static int genpd_add_subdomain(struct generic_pm_domain
*genpd
,
1607 struct generic_pm_domain
*subdomain
)
1609 struct gpd_link
*link
, *itr
;
1612 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(subdomain
)
1613 || genpd
== subdomain
)
1617 * If the domain can be powered on/off in an IRQ safe
1618 * context, ensure that the subdomain can also be
1619 * powered on/off in that context.
1621 if (!genpd_is_irq_safe(genpd
) && genpd_is_irq_safe(subdomain
)) {
1622 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1623 genpd
->name
, subdomain
->name
);
1627 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
1631 genpd_lock(subdomain
);
1632 genpd_lock_nested(genpd
, SINGLE_DEPTH_NESTING
);
1634 if (!genpd_status_on(genpd
) && genpd_status_on(subdomain
)) {
1639 list_for_each_entry(itr
, &genpd
->master_links
, master_node
) {
1640 if (itr
->slave
== subdomain
&& itr
->master
== genpd
) {
1646 link
->master
= genpd
;
1647 list_add_tail(&link
->master_node
, &genpd
->master_links
);
1648 link
->slave
= subdomain
;
1649 list_add_tail(&link
->slave_node
, &subdomain
->slave_links
);
1650 if (genpd_status_on(subdomain
))
1651 genpd_sd_counter_inc(genpd
);
1654 genpd_unlock(genpd
);
1655 genpd_unlock(subdomain
);
1662 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1663 * @genpd: Master PM domain to add the subdomain to.
1664 * @subdomain: Subdomain to be added.
1666 int pm_genpd_add_subdomain(struct generic_pm_domain
*genpd
,
1667 struct generic_pm_domain
*subdomain
)
1671 mutex_lock(&gpd_list_lock
);
1672 ret
= genpd_add_subdomain(genpd
, subdomain
);
1673 mutex_unlock(&gpd_list_lock
);
1677 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain
);
1680 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1681 * @genpd: Master PM domain to remove the subdomain from.
1682 * @subdomain: Subdomain to be removed.
1684 int pm_genpd_remove_subdomain(struct generic_pm_domain
*genpd
,
1685 struct generic_pm_domain
*subdomain
)
1687 struct gpd_link
*l
, *link
;
1690 if (IS_ERR_OR_NULL(genpd
) || IS_ERR_OR_NULL(subdomain
))
1693 genpd_lock(subdomain
);
1694 genpd_lock_nested(genpd
, SINGLE_DEPTH_NESTING
);
1696 if (!list_empty(&subdomain
->master_links
) || subdomain
->device_count
) {
1697 pr_warn("%s: unable to remove subdomain %s\n",
1698 genpd
->name
, subdomain
->name
);
1703 list_for_each_entry_safe(link
, l
, &genpd
->master_links
, master_node
) {
1704 if (link
->slave
!= subdomain
)
1707 list_del(&link
->master_node
);
1708 list_del(&link
->slave_node
);
1710 if (genpd_status_on(subdomain
))
1711 genpd_sd_counter_dec(genpd
);
1718 genpd_unlock(genpd
);
1719 genpd_unlock(subdomain
);
1723 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain
);
1725 static void genpd_free_default_power_state(struct genpd_power_state
*states
,
1726 unsigned int state_count
)
1731 static int genpd_set_default_power_state(struct generic_pm_domain
*genpd
)
1733 struct genpd_power_state
*state
;
1735 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1739 genpd
->states
= state
;
1740 genpd
->state_count
= 1;
1741 genpd
->free_states
= genpd_free_default_power_state
;
1746 static void genpd_lock_init(struct generic_pm_domain
*genpd
)
1748 if (genpd
->flags
& GENPD_FLAG_IRQ_SAFE
) {
1749 spin_lock_init(&genpd
->slock
);
1750 genpd
->lock_ops
= &genpd_spin_ops
;
1752 mutex_init(&genpd
->mlock
);
1753 genpd
->lock_ops
= &genpd_mtx_ops
;
1758 * pm_genpd_init - Initialize a generic I/O PM domain object.
1759 * @genpd: PM domain object to initialize.
1760 * @gov: PM domain governor to associate with the domain (may be NULL).
1761 * @is_off: Initial value of the domain's power_is_off field.
1763 * Returns 0 on successful initialization, else a negative error code.
1765 int pm_genpd_init(struct generic_pm_domain
*genpd
,
1766 struct dev_power_governor
*gov
, bool is_off
)
1770 if (IS_ERR_OR_NULL(genpd
))
1773 INIT_LIST_HEAD(&genpd
->master_links
);
1774 INIT_LIST_HEAD(&genpd
->slave_links
);
1775 INIT_LIST_HEAD(&genpd
->dev_list
);
1776 genpd_lock_init(genpd
);
1778 INIT_WORK(&genpd
->power_off_work
, genpd_power_off_work_fn
);
1779 atomic_set(&genpd
->sd_count
, 0);
1780 genpd
->status
= is_off
? GPD_STATE_POWER_OFF
: GPD_STATE_ACTIVE
;
1781 genpd
->device_count
= 0;
1782 genpd
->max_off_time_ns
= -1;
1783 genpd
->max_off_time_changed
= true;
1784 genpd
->provider
= NULL
;
1785 genpd
->has_provider
= false;
1786 genpd
->accounting_time
= ktime_get();
1787 genpd
->domain
.ops
.runtime_suspend
= genpd_runtime_suspend
;
1788 genpd
->domain
.ops
.runtime_resume
= genpd_runtime_resume
;
1789 genpd
->domain
.ops
.prepare
= genpd_prepare
;
1790 genpd
->domain
.ops
.suspend_noirq
= genpd_suspend_noirq
;
1791 genpd
->domain
.ops
.resume_noirq
= genpd_resume_noirq
;
1792 genpd
->domain
.ops
.freeze_noirq
= genpd_freeze_noirq
;
1793 genpd
->domain
.ops
.thaw_noirq
= genpd_thaw_noirq
;
1794 genpd
->domain
.ops
.poweroff_noirq
= genpd_poweroff_noirq
;
1795 genpd
->domain
.ops
.restore_noirq
= genpd_restore_noirq
;
1796 genpd
->domain
.ops
.complete
= genpd_complete
;
1797 genpd
->domain
.start
= genpd_dev_pm_start
;
1799 if (genpd
->flags
& GENPD_FLAG_PM_CLK
) {
1800 genpd
->dev_ops
.stop
= pm_clk_suspend
;
1801 genpd
->dev_ops
.start
= pm_clk_resume
;
1804 /* Always-on domains must be powered on at initialization. */
1805 if ((genpd_is_always_on(genpd
) || genpd_is_rpm_always_on(genpd
)) &&
1806 !genpd_status_on(genpd
))
1809 if (genpd_is_cpu_domain(genpd
) &&
1810 !zalloc_cpumask_var(&genpd
->cpus
, GFP_KERNEL
))
1813 /* Use only one "off" state if there were no states declared */
1814 if (genpd
->state_count
== 0) {
1815 ret
= genpd_set_default_power_state(genpd
);
1817 if (genpd_is_cpu_domain(genpd
))
1818 free_cpumask_var(genpd
->cpus
);
1821 } else if (!gov
&& genpd
->state_count
> 1) {
1822 pr_warn("%s: no governor for states\n", genpd
->name
);
1825 device_initialize(&genpd
->dev
);
1826 dev_set_name(&genpd
->dev
, "%s", genpd
->name
);
1828 mutex_lock(&gpd_list_lock
);
1829 list_add(&genpd
->gpd_list_node
, &gpd_list
);
1830 mutex_unlock(&gpd_list_lock
);
1834 EXPORT_SYMBOL_GPL(pm_genpd_init
);
1836 static int genpd_remove(struct generic_pm_domain
*genpd
)
1838 struct gpd_link
*l
, *link
;
1840 if (IS_ERR_OR_NULL(genpd
))
1845 if (genpd
->has_provider
) {
1846 genpd_unlock(genpd
);
1847 pr_err("Provider present, unable to remove %s\n", genpd
->name
);
1851 if (!list_empty(&genpd
->master_links
) || genpd
->device_count
) {
1852 genpd_unlock(genpd
);
1853 pr_err("%s: unable to remove %s\n", __func__
, genpd
->name
);
1857 list_for_each_entry_safe(link
, l
, &genpd
->slave_links
, slave_node
) {
1858 list_del(&link
->master_node
);
1859 list_del(&link
->slave_node
);
1863 list_del(&genpd
->gpd_list_node
);
1864 genpd_unlock(genpd
);
1865 cancel_work_sync(&genpd
->power_off_work
);
1866 if (genpd_is_cpu_domain(genpd
))
1867 free_cpumask_var(genpd
->cpus
);
1868 if (genpd
->free_states
)
1869 genpd
->free_states(genpd
->states
, genpd
->state_count
);
1871 pr_debug("%s: removed %s\n", __func__
, genpd
->name
);
1877 * pm_genpd_remove - Remove a generic I/O PM domain
1878 * @genpd: Pointer to PM domain that is to be removed.
1880 * To remove the PM domain, this function:
1881 * - Removes the PM domain as a subdomain to any parent domains,
1883 * - Removes the PM domain from the list of registered PM domains.
1885 * The PM domain will only be removed, if the associated provider has
1886 * been removed, it is not a parent to any other PM domain and has no
1887 * devices associated with it.
1889 int pm_genpd_remove(struct generic_pm_domain
*genpd
)
1893 mutex_lock(&gpd_list_lock
);
1894 ret
= genpd_remove(genpd
);
1895 mutex_unlock(&gpd_list_lock
);
1899 EXPORT_SYMBOL_GPL(pm_genpd_remove
);
1901 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1904 * Device Tree based PM domain providers.
1906 * The code below implements generic device tree based PM domain providers that
1907 * bind device tree nodes with generic PM domains registered in the system.
1909 * Any driver that registers generic PM domains and needs to support binding of
1910 * devices to these domains is supposed to register a PM domain provider, which
1911 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1913 * Two simple mapping functions have been provided for convenience:
1914 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1915 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1920 * struct of_genpd_provider - PM domain provider registration structure
1921 * @link: Entry in global list of PM domain providers
1922 * @node: Pointer to device tree node of PM domain provider
1923 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1925 * @data: context pointer to be passed into @xlate callback
1927 struct of_genpd_provider
{
1928 struct list_head link
;
1929 struct device_node
*node
;
1930 genpd_xlate_t xlate
;
1934 /* List of registered PM domain providers. */
1935 static LIST_HEAD(of_genpd_providers
);
1936 /* Mutex to protect the list above. */
1937 static DEFINE_MUTEX(of_genpd_mutex
);
1940 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1941 * @genpdspec: OF phandle args to map into a PM domain
1942 * @data: xlate function private data - pointer to struct generic_pm_domain
1944 * This is a generic xlate function that can be used to model PM domains that
1945 * have their own device tree nodes. The private data of xlate function needs
1946 * to be a valid pointer to struct generic_pm_domain.
1948 static struct generic_pm_domain
*genpd_xlate_simple(
1949 struct of_phandle_args
*genpdspec
,
1956 * genpd_xlate_onecell() - Xlate function using a single index.
1957 * @genpdspec: OF phandle args to map into a PM domain
1958 * @data: xlate function private data - pointer to struct genpd_onecell_data
1960 * This is a generic xlate function that can be used to model simple PM domain
1961 * controllers that have one device tree node and provide multiple PM domains.
1962 * A single cell is used as an index into an array of PM domains specified in
1963 * the genpd_onecell_data struct when registering the provider.
1965 static struct generic_pm_domain
*genpd_xlate_onecell(
1966 struct of_phandle_args
*genpdspec
,
1969 struct genpd_onecell_data
*genpd_data
= data
;
1970 unsigned int idx
= genpdspec
->args
[0];
1972 if (genpdspec
->args_count
!= 1)
1973 return ERR_PTR(-EINVAL
);
1975 if (idx
>= genpd_data
->num_domains
) {
1976 pr_err("%s: invalid domain index %u\n", __func__
, idx
);
1977 return ERR_PTR(-EINVAL
);
1980 if (!genpd_data
->domains
[idx
])
1981 return ERR_PTR(-ENOENT
);
1983 return genpd_data
->domains
[idx
];
1987 * genpd_add_provider() - Register a PM domain provider for a node
1988 * @np: Device node pointer associated with the PM domain provider.
1989 * @xlate: Callback for decoding PM domain from phandle arguments.
1990 * @data: Context pointer for @xlate callback.
1992 static int genpd_add_provider(struct device_node
*np
, genpd_xlate_t xlate
,
1995 struct of_genpd_provider
*cp
;
1997 cp
= kzalloc(sizeof(*cp
), GFP_KERNEL
);
2001 cp
->node
= of_node_get(np
);
2005 mutex_lock(&of_genpd_mutex
);
2006 list_add(&cp
->link
, &of_genpd_providers
);
2007 mutex_unlock(&of_genpd_mutex
);
2008 pr_debug("Added domain provider from %pOF\n", np
);
2013 static bool genpd_present(const struct generic_pm_domain
*genpd
)
2015 const struct generic_pm_domain
*gpd
;
2017 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
)
2024 * of_genpd_add_provider_simple() - Register a simple PM domain provider
2025 * @np: Device node pointer associated with the PM domain provider.
2026 * @genpd: Pointer to PM domain associated with the PM domain provider.
2028 int of_genpd_add_provider_simple(struct device_node
*np
,
2029 struct generic_pm_domain
*genpd
)
2036 mutex_lock(&gpd_list_lock
);
2038 if (!genpd_present(genpd
))
2041 genpd
->dev
.of_node
= np
;
2043 /* Parse genpd OPP table */
2044 if (genpd
->set_performance_state
) {
2045 ret
= dev_pm_opp_of_add_table(&genpd
->dev
);
2047 dev_err(&genpd
->dev
, "Failed to add OPP table: %d\n",
2053 * Save table for faster processing while setting performance
2056 genpd
->opp_table
= dev_pm_opp_get_opp_table(&genpd
->dev
);
2057 WARN_ON(!genpd
->opp_table
);
2060 ret
= genpd_add_provider(np
, genpd_xlate_simple
, genpd
);
2062 if (genpd
->set_performance_state
) {
2063 dev_pm_opp_put_opp_table(genpd
->opp_table
);
2064 dev_pm_opp_of_remove_table(&genpd
->dev
);
2070 genpd
->provider
= &np
->fwnode
;
2071 genpd
->has_provider
= true;
2074 mutex_unlock(&gpd_list_lock
);
2078 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple
);
2081 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
2082 * @np: Device node pointer associated with the PM domain provider.
2083 * @data: Pointer to the data associated with the PM domain provider.
2085 int of_genpd_add_provider_onecell(struct device_node
*np
,
2086 struct genpd_onecell_data
*data
)
2088 struct generic_pm_domain
*genpd
;
2095 mutex_lock(&gpd_list_lock
);
2098 data
->xlate
= genpd_xlate_onecell
;
2100 for (i
= 0; i
< data
->num_domains
; i
++) {
2101 genpd
= data
->domains
[i
];
2105 if (!genpd_present(genpd
))
2108 genpd
->dev
.of_node
= np
;
2110 /* Parse genpd OPP table */
2111 if (genpd
->set_performance_state
) {
2112 ret
= dev_pm_opp_of_add_table_indexed(&genpd
->dev
, i
);
2114 dev_err(&genpd
->dev
, "Failed to add OPP table for index %d: %d\n",
2120 * Save table for faster processing while setting
2121 * performance state.
2123 genpd
->opp_table
= dev_pm_opp_get_opp_table_indexed(&genpd
->dev
, i
);
2124 WARN_ON(!genpd
->opp_table
);
2127 genpd
->provider
= &np
->fwnode
;
2128 genpd
->has_provider
= true;
2131 ret
= genpd_add_provider(np
, data
->xlate
, data
);
2135 mutex_unlock(&gpd_list_lock
);
2141 genpd
= data
->domains
[i
];
2146 genpd
->provider
= NULL
;
2147 genpd
->has_provider
= false;
2149 if (genpd
->set_performance_state
) {
2150 dev_pm_opp_put_opp_table(genpd
->opp_table
);
2151 dev_pm_opp_of_remove_table(&genpd
->dev
);
2155 mutex_unlock(&gpd_list_lock
);
2159 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell
);
2162 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2163 * @np: Device node pointer associated with the PM domain provider
2165 void of_genpd_del_provider(struct device_node
*np
)
2167 struct of_genpd_provider
*cp
, *tmp
;
2168 struct generic_pm_domain
*gpd
;
2170 mutex_lock(&gpd_list_lock
);
2171 mutex_lock(&of_genpd_mutex
);
2172 list_for_each_entry_safe(cp
, tmp
, &of_genpd_providers
, link
) {
2173 if (cp
->node
== np
) {
2175 * For each PM domain associated with the
2176 * provider, set the 'has_provider' to false
2177 * so that the PM domain can be safely removed.
2179 list_for_each_entry(gpd
, &gpd_list
, gpd_list_node
) {
2180 if (gpd
->provider
== &np
->fwnode
) {
2181 gpd
->has_provider
= false;
2183 if (!gpd
->set_performance_state
)
2186 dev_pm_opp_put_opp_table(gpd
->opp_table
);
2187 dev_pm_opp_of_remove_table(&gpd
->dev
);
2191 list_del(&cp
->link
);
2192 of_node_put(cp
->node
);
2197 mutex_unlock(&of_genpd_mutex
);
2198 mutex_unlock(&gpd_list_lock
);
2200 EXPORT_SYMBOL_GPL(of_genpd_del_provider
);
2203 * genpd_get_from_provider() - Look-up PM domain
2204 * @genpdspec: OF phandle args to use for look-up
2206 * Looks for a PM domain provider under the node specified by @genpdspec and if
2207 * found, uses xlate function of the provider to map phandle args to a PM
2210 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2213 static struct generic_pm_domain
*genpd_get_from_provider(
2214 struct of_phandle_args
*genpdspec
)
2216 struct generic_pm_domain
*genpd
= ERR_PTR(-ENOENT
);
2217 struct of_genpd_provider
*provider
;
2220 return ERR_PTR(-EINVAL
);
2222 mutex_lock(&of_genpd_mutex
);
2224 /* Check if we have such a provider in our array */
2225 list_for_each_entry(provider
, &of_genpd_providers
, link
) {
2226 if (provider
->node
== genpdspec
->np
)
2227 genpd
= provider
->xlate(genpdspec
, provider
->data
);
2232 mutex_unlock(&of_genpd_mutex
);
2238 * of_genpd_add_device() - Add a device to an I/O PM domain
2239 * @genpdspec: OF phandle args to use for look-up PM domain
2240 * @dev: Device to be added.
2242 * Looks-up an I/O PM domain based upon phandle args provided and adds
2243 * the device to the PM domain. Returns a negative error code on failure.
2245 int of_genpd_add_device(struct of_phandle_args
*genpdspec
, struct device
*dev
)
2247 struct generic_pm_domain
*genpd
;
2250 mutex_lock(&gpd_list_lock
);
2252 genpd
= genpd_get_from_provider(genpdspec
);
2253 if (IS_ERR(genpd
)) {
2254 ret
= PTR_ERR(genpd
);
2258 ret
= genpd_add_device(genpd
, dev
, dev
);
2261 mutex_unlock(&gpd_list_lock
);
2265 EXPORT_SYMBOL_GPL(of_genpd_add_device
);
2268 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2269 * @parent_spec: OF phandle args to use for parent PM domain look-up
2270 * @subdomain_spec: OF phandle args to use for subdomain look-up
2272 * Looks-up a parent PM domain and subdomain based upon phandle args
2273 * provided and adds the subdomain to the parent PM domain. Returns a
2274 * negative error code on failure.
2276 int of_genpd_add_subdomain(struct of_phandle_args
*parent_spec
,
2277 struct of_phandle_args
*subdomain_spec
)
2279 struct generic_pm_domain
*parent
, *subdomain
;
2282 mutex_lock(&gpd_list_lock
);
2284 parent
= genpd_get_from_provider(parent_spec
);
2285 if (IS_ERR(parent
)) {
2286 ret
= PTR_ERR(parent
);
2290 subdomain
= genpd_get_from_provider(subdomain_spec
);
2291 if (IS_ERR(subdomain
)) {
2292 ret
= PTR_ERR(subdomain
);
2296 ret
= genpd_add_subdomain(parent
, subdomain
);
2299 mutex_unlock(&gpd_list_lock
);
2303 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain
);
2306 * of_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
2307 * @parent_spec: OF phandle args to use for parent PM domain look-up
2308 * @subdomain_spec: OF phandle args to use for subdomain look-up
2310 * Looks-up a parent PM domain and subdomain based upon phandle args
2311 * provided and removes the subdomain from the parent PM domain. Returns a
2312 * negative error code on failure.
2314 int of_genpd_remove_subdomain(struct of_phandle_args
*parent_spec
,
2315 struct of_phandle_args
*subdomain_spec
)
2317 struct generic_pm_domain
*parent
, *subdomain
;
2320 mutex_lock(&gpd_list_lock
);
2322 parent
= genpd_get_from_provider(parent_spec
);
2323 if (IS_ERR(parent
)) {
2324 ret
= PTR_ERR(parent
);
2328 subdomain
= genpd_get_from_provider(subdomain_spec
);
2329 if (IS_ERR(subdomain
)) {
2330 ret
= PTR_ERR(subdomain
);
2334 ret
= pm_genpd_remove_subdomain(parent
, subdomain
);
2337 mutex_unlock(&gpd_list_lock
);
2341 EXPORT_SYMBOL_GPL(of_genpd_remove_subdomain
);
2344 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2345 * @provider: Pointer to device structure associated with provider
2347 * Find the last PM domain that was added by a particular provider and
2348 * remove this PM domain from the list of PM domains. The provider is
2349 * identified by the 'provider' device structure that is passed. The PM
2350 * domain will only be removed, if the provider associated with domain
2353 * Returns a valid pointer to struct generic_pm_domain on success or
2354 * ERR_PTR() on failure.
2356 struct generic_pm_domain
*of_genpd_remove_last(struct device_node
*np
)
2358 struct generic_pm_domain
*gpd
, *tmp
, *genpd
= ERR_PTR(-ENOENT
);
2361 if (IS_ERR_OR_NULL(np
))
2362 return ERR_PTR(-EINVAL
);
2364 mutex_lock(&gpd_list_lock
);
2365 list_for_each_entry_safe(gpd
, tmp
, &gpd_list
, gpd_list_node
) {
2366 if (gpd
->provider
== &np
->fwnode
) {
2367 ret
= genpd_remove(gpd
);
2368 genpd
= ret
? ERR_PTR(ret
) : gpd
;
2372 mutex_unlock(&gpd_list_lock
);
2376 EXPORT_SYMBOL_GPL(of_genpd_remove_last
);
2378 static void genpd_release_dev(struct device
*dev
)
2380 of_node_put(dev
->of_node
);
2384 static struct bus_type genpd_bus_type
= {
2389 * genpd_dev_pm_detach - Detach a device from its PM domain.
2390 * @dev: Device to detach.
2391 * @power_off: Currently not used
2393 * Try to locate a corresponding generic PM domain, which the device was
2394 * attached to previously. If such is found, the device is detached from it.
2396 static void genpd_dev_pm_detach(struct device
*dev
, bool power_off
)
2398 struct generic_pm_domain
*pd
;
2402 pd
= dev_to_genpd(dev
);
2406 dev_dbg(dev
, "removing from PM domain %s\n", pd
->name
);
2408 for (i
= 1; i
< GENPD_RETRY_MAX_MS
; i
<<= 1) {
2409 ret
= genpd_remove_device(pd
, dev
);
2418 dev_err(dev
, "failed to remove from PM domain %s: %d",
2423 /* Check if PM domain can be powered off after removing this device. */
2424 genpd_queue_power_off_work(pd
);
2426 /* Unregister the device if it was created by genpd. */
2427 if (dev
->bus
== &genpd_bus_type
)
2428 device_unregister(dev
);
2431 static void genpd_dev_pm_sync(struct device
*dev
)
2433 struct generic_pm_domain
*pd
;
2435 pd
= dev_to_genpd(dev
);
2439 genpd_queue_power_off_work(pd
);
2442 static int __genpd_dev_pm_attach(struct device
*dev
, struct device
*base_dev
,
2443 unsigned int index
, bool power_on
)
2445 struct of_phandle_args pd_args
;
2446 struct generic_pm_domain
*pd
;
2449 ret
= of_parse_phandle_with_args(dev
->of_node
, "power-domains",
2450 "#power-domain-cells", index
, &pd_args
);
2454 mutex_lock(&gpd_list_lock
);
2455 pd
= genpd_get_from_provider(&pd_args
);
2456 of_node_put(pd_args
.np
);
2458 mutex_unlock(&gpd_list_lock
);
2459 dev_dbg(dev
, "%s() failed to find PM domain: %ld\n",
2460 __func__
, PTR_ERR(pd
));
2461 return driver_deferred_probe_check_state(base_dev
);
2464 dev_dbg(dev
, "adding to PM domain %s\n", pd
->name
);
2466 ret
= genpd_add_device(pd
, dev
, base_dev
);
2467 mutex_unlock(&gpd_list_lock
);
2470 if (ret
!= -EPROBE_DEFER
)
2471 dev_err(dev
, "failed to add to PM domain %s: %d",
2476 dev
->pm_domain
->detach
= genpd_dev_pm_detach
;
2477 dev
->pm_domain
->sync
= genpd_dev_pm_sync
;
2481 ret
= genpd_power_on(pd
, 0);
2486 genpd_remove_device(pd
, dev
);
2488 return ret
? -EPROBE_DEFER
: 1;
2492 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2493 * @dev: Device to attach.
2495 * Parse device's OF node to find a PM domain specifier. If such is found,
2496 * attaches the device to retrieved pm_domain ops.
2498 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2499 * PM domain or when multiple power-domains exists for it, else a negative error
2500 * code. Note that if a power-domain exists for the device, but it cannot be
2501 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2502 * not probed and to re-try again later.
2504 int genpd_dev_pm_attach(struct device
*dev
)
2510 * Devices with multiple PM domains must be attached separately, as we
2511 * can only attach one PM domain per device.
2513 if (of_count_phandle_with_args(dev
->of_node
, "power-domains",
2514 "#power-domain-cells") != 1)
2517 return __genpd_dev_pm_attach(dev
, dev
, 0, true);
2519 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach
);
2522 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2523 * @dev: The device used to lookup the PM domain.
2524 * @index: The index of the PM domain.
2526 * Parse device's OF node to find a PM domain specifier at the provided @index.
2527 * If such is found, creates a virtual device and attaches it to the retrieved
2528 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2529 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2531 * Returns the created virtual device if successfully attached PM domain, NULL
2532 * when the device don't need a PM domain, else an ERR_PTR() in case of
2533 * failures. If a power-domain exists for the device, but cannot be found or
2534 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2535 * is not probed and to re-try again later.
2537 struct device
*genpd_dev_pm_attach_by_id(struct device
*dev
,
2540 struct device
*virt_dev
;
2547 /* Verify that the index is within a valid range. */
2548 num_domains
= of_count_phandle_with_args(dev
->of_node
, "power-domains",
2549 "#power-domain-cells");
2550 if (index
>= num_domains
)
2553 /* Allocate and register device on the genpd bus. */
2554 virt_dev
= kzalloc(sizeof(*virt_dev
), GFP_KERNEL
);
2556 return ERR_PTR(-ENOMEM
);
2558 dev_set_name(virt_dev
, "genpd:%u:%s", index
, dev_name(dev
));
2559 virt_dev
->bus
= &genpd_bus_type
;
2560 virt_dev
->release
= genpd_release_dev
;
2561 virt_dev
->of_node
= of_node_get(dev
->of_node
);
2563 ret
= device_register(virt_dev
);
2565 put_device(virt_dev
);
2566 return ERR_PTR(ret
);
2569 /* Try to attach the device to the PM domain at the specified index. */
2570 ret
= __genpd_dev_pm_attach(virt_dev
, dev
, index
, false);
2572 device_unregister(virt_dev
);
2573 return ret
? ERR_PTR(ret
) : NULL
;
2576 pm_runtime_enable(virt_dev
);
2577 genpd_queue_power_off_work(dev_to_genpd(virt_dev
));
2581 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id
);
2584 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
2585 * @dev: The device used to lookup the PM domain.
2586 * @name: The name of the PM domain.
2588 * Parse device's OF node to find a PM domain specifier using the
2589 * power-domain-names DT property. For further description see
2590 * genpd_dev_pm_attach_by_id().
2592 struct device
*genpd_dev_pm_attach_by_name(struct device
*dev
, const char *name
)
2599 index
= of_property_match_string(dev
->of_node
, "power-domain-names",
2604 return genpd_dev_pm_attach_by_id(dev
, index
);
2607 static const struct of_device_id idle_state_match
[] = {
2608 { .compatible
= "domain-idle-state", },
2612 static int genpd_parse_state(struct genpd_power_state
*genpd_state
,
2613 struct device_node
*state_node
)
2617 u32 entry_latency
, exit_latency
;
2619 err
= of_property_read_u32(state_node
, "entry-latency-us",
2622 pr_debug(" * %pOF missing entry-latency-us property\n",
2627 err
= of_property_read_u32(state_node
, "exit-latency-us",
2630 pr_debug(" * %pOF missing exit-latency-us property\n",
2635 err
= of_property_read_u32(state_node
, "min-residency-us", &residency
);
2637 genpd_state
->residency_ns
= 1000 * residency
;
2639 genpd_state
->power_on_latency_ns
= 1000 * exit_latency
;
2640 genpd_state
->power_off_latency_ns
= 1000 * entry_latency
;
2641 genpd_state
->fwnode
= &state_node
->fwnode
;
2646 static int genpd_iterate_idle_states(struct device_node
*dn
,
2647 struct genpd_power_state
*states
)
2650 struct of_phandle_iterator it
;
2651 struct device_node
*np
;
2654 ret
= of_count_phandle_with_args(dn
, "domain-idle-states", NULL
);
2656 return ret
== -ENOENT
? 0 : ret
;
2658 /* Loop over the phandles until all the requested entry is found */
2659 of_for_each_phandle(&it
, ret
, dn
, "domain-idle-states", NULL
, 0) {
2661 if (!of_match_node(idle_state_match
, np
))
2664 ret
= genpd_parse_state(&states
[i
], np
);
2666 pr_err("Parsing idle state node %pOF failed with err %d\n",
2679 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2681 * @dn: The genpd device node
2682 * @states: The pointer to which the state array will be saved.
2683 * @n: The count of elements in the array returned from this function.
2685 * Returns the device states parsed from the OF node. The memory for the states
2686 * is allocated by this function and is the responsibility of the caller to
2687 * free the memory after use. If any or zero compatible domain idle states is
2688 * found it returns 0 and in case of errors, a negative error code is returned.
2690 int of_genpd_parse_idle_states(struct device_node
*dn
,
2691 struct genpd_power_state
**states
, int *n
)
2693 struct genpd_power_state
*st
;
2696 ret
= genpd_iterate_idle_states(dn
, NULL
);
2706 st
= kcalloc(ret
, sizeof(*st
), GFP_KERNEL
);
2710 ret
= genpd_iterate_idle_states(dn
, st
);
2713 return ret
< 0 ? ret
: -EINVAL
;
2721 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states
);
2724 * pm_genpd_opp_to_performance_state - Gets performance state of the genpd from its OPP node.
2726 * @genpd_dev: Genpd's device for which the performance-state needs to be found.
2727 * @opp: struct dev_pm_opp of the OPP for which we need to find performance
2730 * Returns performance state encoded in the OPP of the genpd. This calls
2731 * platform specific genpd->opp_to_performance_state() callback to translate
2732 * power domain OPP to performance state.
2734 * Returns performance state on success and 0 on failure.
2736 unsigned int pm_genpd_opp_to_performance_state(struct device
*genpd_dev
,
2737 struct dev_pm_opp
*opp
)
2739 struct generic_pm_domain
*genpd
= NULL
;
2742 genpd
= container_of(genpd_dev
, struct generic_pm_domain
, dev
);
2744 if (unlikely(!genpd
->opp_to_performance_state
))
2748 state
= genpd
->opp_to_performance_state(genpd
, opp
);
2749 genpd_unlock(genpd
);
2753 EXPORT_SYMBOL_GPL(pm_genpd_opp_to_performance_state
);
2755 static int __init
genpd_bus_init(void)
2757 return bus_register(&genpd_bus_type
);
2759 core_initcall(genpd_bus_init
);
2761 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2764 /*** debugfs support ***/
2766 #ifdef CONFIG_DEBUG_FS
2767 #include <linux/pm.h>
2768 #include <linux/device.h>
2769 #include <linux/debugfs.h>
2770 #include <linux/seq_file.h>
2771 #include <linux/init.h>
2772 #include <linux/kobject.h>
2773 static struct dentry
*genpd_debugfs_dir
;
2776 * TODO: This function is a slightly modified version of rtpm_status_show
2777 * from sysfs.c, so generalize it.
2779 static void rtpm_status_str(struct seq_file
*s
, struct device
*dev
)
2781 static const char * const status_lookup
[] = {
2782 [RPM_ACTIVE
] = "active",
2783 [RPM_RESUMING
] = "resuming",
2784 [RPM_SUSPENDED
] = "suspended",
2785 [RPM_SUSPENDING
] = "suspending"
2789 if (dev
->power
.runtime_error
)
2791 else if (dev
->power
.disable_depth
)
2793 else if (dev
->power
.runtime_status
< ARRAY_SIZE(status_lookup
))
2794 p
= status_lookup
[dev
->power
.runtime_status
];
2801 static int genpd_summary_one(struct seq_file
*s
,
2802 struct generic_pm_domain
*genpd
)
2804 static const char * const status_lookup
[] = {
2805 [GPD_STATE_ACTIVE
] = "on",
2806 [GPD_STATE_POWER_OFF
] = "off"
2808 struct pm_domain_data
*pm_data
;
2809 const char *kobj_path
;
2810 struct gpd_link
*link
;
2814 ret
= genpd_lock_interruptible(genpd
);
2816 return -ERESTARTSYS
;
2818 if (WARN_ON(genpd
->status
>= ARRAY_SIZE(status_lookup
)))
2820 if (!genpd_status_on(genpd
))
2821 snprintf(state
, sizeof(state
), "%s-%u",
2822 status_lookup
[genpd
->status
], genpd
->state_idx
);
2824 snprintf(state
, sizeof(state
), "%s",
2825 status_lookup
[genpd
->status
]);
2826 seq_printf(s
, "%-30s %-15s ", genpd
->name
, state
);
2829 * Modifications on the list require holding locks on both
2830 * master and slave, so we are safe.
2831 * Also genpd->name is immutable.
2833 list_for_each_entry(link
, &genpd
->master_links
, master_node
) {
2834 seq_printf(s
, "%s", link
->slave
->name
);
2835 if (!list_is_last(&link
->master_node
, &genpd
->master_links
))
2839 list_for_each_entry(pm_data
, &genpd
->dev_list
, list_node
) {
2840 kobj_path
= kobject_get_path(&pm_data
->dev
->kobj
,
2841 genpd_is_irq_safe(genpd
) ?
2842 GFP_ATOMIC
: GFP_KERNEL
);
2843 if (kobj_path
== NULL
)
2846 seq_printf(s
, "\n %-50s ", kobj_path
);
2847 rtpm_status_str(s
, pm_data
->dev
);
2853 genpd_unlock(genpd
);
2858 static int summary_show(struct seq_file
*s
, void *data
)
2860 struct generic_pm_domain
*genpd
;
2863 seq_puts(s
, "domain status slaves\n");
2864 seq_puts(s
, " /device runtime status\n");
2865 seq_puts(s
, "----------------------------------------------------------------------\n");
2867 ret
= mutex_lock_interruptible(&gpd_list_lock
);
2869 return -ERESTARTSYS
;
2871 list_for_each_entry(genpd
, &gpd_list
, gpd_list_node
) {
2872 ret
= genpd_summary_one(s
, genpd
);
2876 mutex_unlock(&gpd_list_lock
);
2881 static int status_show(struct seq_file
*s
, void *data
)
2883 static const char * const status_lookup
[] = {
2884 [GPD_STATE_ACTIVE
] = "on",
2885 [GPD_STATE_POWER_OFF
] = "off"
2888 struct generic_pm_domain
*genpd
= s
->private;
2891 ret
= genpd_lock_interruptible(genpd
);
2893 return -ERESTARTSYS
;
2895 if (WARN_ON_ONCE(genpd
->status
>= ARRAY_SIZE(status_lookup
)))
2898 if (genpd
->status
== GPD_STATE_POWER_OFF
)
2899 seq_printf(s
, "%s-%u\n", status_lookup
[genpd
->status
],
2902 seq_printf(s
, "%s\n", status_lookup
[genpd
->status
]);
2904 genpd_unlock(genpd
);
2908 static int sub_domains_show(struct seq_file
*s
, void *data
)
2910 struct generic_pm_domain
*genpd
= s
->private;
2911 struct gpd_link
*link
;
2914 ret
= genpd_lock_interruptible(genpd
);
2916 return -ERESTARTSYS
;
2918 list_for_each_entry(link
, &genpd
->master_links
, master_node
)
2919 seq_printf(s
, "%s\n", link
->slave
->name
);
2921 genpd_unlock(genpd
);
2925 static int idle_states_show(struct seq_file
*s
, void *data
)
2927 struct generic_pm_domain
*genpd
= s
->private;
2931 ret
= genpd_lock_interruptible(genpd
);
2933 return -ERESTARTSYS
;
2935 seq_puts(s
, "State Time Spent(ms)\n");
2937 for (i
= 0; i
< genpd
->state_count
; i
++) {
2941 if ((genpd
->status
== GPD_STATE_POWER_OFF
) &&
2942 (genpd
->state_idx
== i
))
2943 delta
= ktime_sub(ktime_get(), genpd
->accounting_time
);
2945 msecs
= ktime_to_ms(
2946 ktime_add(genpd
->states
[i
].idle_time
, delta
));
2947 seq_printf(s
, "S%-13i %lld\n", i
, msecs
);
2950 genpd_unlock(genpd
);
2954 static int active_time_show(struct seq_file
*s
, void *data
)
2956 struct generic_pm_domain
*genpd
= s
->private;
2960 ret
= genpd_lock_interruptible(genpd
);
2962 return -ERESTARTSYS
;
2964 if (genpd
->status
== GPD_STATE_ACTIVE
)
2965 delta
= ktime_sub(ktime_get(), genpd
->accounting_time
);
2967 seq_printf(s
, "%lld ms\n", ktime_to_ms(
2968 ktime_add(genpd
->on_time
, delta
)));
2970 genpd_unlock(genpd
);
2974 static int total_idle_time_show(struct seq_file
*s
, void *data
)
2976 struct generic_pm_domain
*genpd
= s
->private;
2977 ktime_t delta
= 0, total
= 0;
2981 ret
= genpd_lock_interruptible(genpd
);
2983 return -ERESTARTSYS
;
2985 for (i
= 0; i
< genpd
->state_count
; i
++) {
2987 if ((genpd
->status
== GPD_STATE_POWER_OFF
) &&
2988 (genpd
->state_idx
== i
))
2989 delta
= ktime_sub(ktime_get(), genpd
->accounting_time
);
2991 total
= ktime_add(total
, genpd
->states
[i
].idle_time
);
2993 total
= ktime_add(total
, delta
);
2995 seq_printf(s
, "%lld ms\n", ktime_to_ms(total
));
2997 genpd_unlock(genpd
);
3002 static int devices_show(struct seq_file
*s
, void *data
)
3004 struct generic_pm_domain
*genpd
= s
->private;
3005 struct pm_domain_data
*pm_data
;
3006 const char *kobj_path
;
3009 ret
= genpd_lock_interruptible(genpd
);
3011 return -ERESTARTSYS
;
3013 list_for_each_entry(pm_data
, &genpd
->dev_list
, list_node
) {
3014 kobj_path
= kobject_get_path(&pm_data
->dev
->kobj
,
3015 genpd_is_irq_safe(genpd
) ?
3016 GFP_ATOMIC
: GFP_KERNEL
);
3017 if (kobj_path
== NULL
)
3020 seq_printf(s
, "%s\n", kobj_path
);
3024 genpd_unlock(genpd
);
3028 static int perf_state_show(struct seq_file
*s
, void *data
)
3030 struct generic_pm_domain
*genpd
= s
->private;
3032 if (genpd_lock_interruptible(genpd
))
3033 return -ERESTARTSYS
;
3035 seq_printf(s
, "%u\n", genpd
->performance_state
);
3037 genpd_unlock(genpd
);
3041 DEFINE_SHOW_ATTRIBUTE(summary
);
3042 DEFINE_SHOW_ATTRIBUTE(status
);
3043 DEFINE_SHOW_ATTRIBUTE(sub_domains
);
3044 DEFINE_SHOW_ATTRIBUTE(idle_states
);
3045 DEFINE_SHOW_ATTRIBUTE(active_time
);
3046 DEFINE_SHOW_ATTRIBUTE(total_idle_time
);
3047 DEFINE_SHOW_ATTRIBUTE(devices
);
3048 DEFINE_SHOW_ATTRIBUTE(perf_state
);
3050 static int __init
genpd_debug_init(void)
3053 struct generic_pm_domain
*genpd
;
3055 genpd_debugfs_dir
= debugfs_create_dir("pm_genpd", NULL
);
3057 debugfs_create_file("pm_genpd_summary", S_IRUGO
, genpd_debugfs_dir
,
3058 NULL
, &summary_fops
);
3060 list_for_each_entry(genpd
, &gpd_list
, gpd_list_node
) {
3061 d
= debugfs_create_dir(genpd
->name
, genpd_debugfs_dir
);
3063 debugfs_create_file("current_state", 0444,
3064 d
, genpd
, &status_fops
);
3065 debugfs_create_file("sub_domains", 0444,
3066 d
, genpd
, &sub_domains_fops
);
3067 debugfs_create_file("idle_states", 0444,
3068 d
, genpd
, &idle_states_fops
);
3069 debugfs_create_file("active_time", 0444,
3070 d
, genpd
, &active_time_fops
);
3071 debugfs_create_file("total_idle_time", 0444,
3072 d
, genpd
, &total_idle_time_fops
);
3073 debugfs_create_file("devices", 0444,
3074 d
, genpd
, &devices_fops
);
3075 if (genpd
->set_performance_state
)
3076 debugfs_create_file("perf_state", 0444,
3077 d
, genpd
, &perf_state_fops
);
3082 late_initcall(genpd_debug_init
);
3084 static void __exit
genpd_debug_exit(void)
3086 debugfs_remove_recursive(genpd_debugfs_dir
);
3088 __exitcall(genpd_debug_exit
);
3089 #endif /* CONFIG_DEBUG_FS */