2 * drivers/base/power/domain.c - Common code related to device power domains.
6 * This file is released under the GPLv2.
9 #define pr_fmt(fmt) "PM: " fmt
11 #include <linux/delay.h>
12 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_opp.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/pm_domain.h>
18 #include <linux/pm_qos.h>
19 #include <linux/pm_clock.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/sched.h>
23 #include <linux/suspend.h>
24 #include <linux/export.h>
28 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
30 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
32 type (*__routine)(struct device *__d); \
33 type __ret = (type)0; \
35 __routine = genpd->dev_ops.callback; \
37 __ret = __routine(dev); \
42 static LIST_HEAD(gpd_list);
43 static DEFINE_MUTEX(gpd_list_lock);
45 struct genpd_lock_ops {
46 void (*lock)(struct generic_pm_domain *genpd);
47 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
48 int (*lock_interruptible)(struct generic_pm_domain *genpd);
49 void (*unlock)(struct generic_pm_domain *genpd);
52 static void genpd_lock_mtx(struct generic_pm_domain *genpd)
54 mutex_lock(&genpd->mlock);
57 static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
60 mutex_lock_nested(&genpd->mlock, depth);
63 static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
65 return mutex_lock_interruptible(&genpd->mlock);
68 static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
70 return mutex_unlock(&genpd->mlock);
73 static const struct genpd_lock_ops genpd_mtx_ops = {
74 .lock = genpd_lock_mtx,
75 .lock_nested = genpd_lock_nested_mtx,
76 .lock_interruptible = genpd_lock_interruptible_mtx,
77 .unlock = genpd_unlock_mtx,
80 static void genpd_lock_spin(struct generic_pm_domain *genpd)
81 __acquires(&genpd->slock)
85 spin_lock_irqsave(&genpd->slock, flags);
86 genpd->lock_flags = flags;
89 static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
91 __acquires(&genpd->slock)
95 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
96 genpd->lock_flags = flags;
99 static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
100 __acquires(&genpd->slock)
104 spin_lock_irqsave(&genpd->slock, flags);
105 genpd->lock_flags = flags;
109 static void genpd_unlock_spin(struct generic_pm_domain *genpd)
110 __releases(&genpd->slock)
112 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
115 static const struct genpd_lock_ops genpd_spin_ops = {
116 .lock = genpd_lock_spin,
117 .lock_nested = genpd_lock_nested_spin,
118 .lock_interruptible = genpd_lock_interruptible_spin,
119 .unlock = genpd_unlock_spin,
122 #define genpd_lock(p) p->lock_ops->lock(p)
123 #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
124 #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
125 #define genpd_unlock(p) p->lock_ops->unlock(p)
127 #define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
128 #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
129 #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
130 #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
132 static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
133 const struct generic_pm_domain *genpd)
137 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
140 * Warn once if an IRQ safe device is attached to a no sleep domain, as
141 * to indicate a suboptimal configuration for PM. For an always on
142 * domain this isn't case, thus don't warn.
144 if (ret && !genpd_is_always_on(genpd))
145 dev_warn_once(dev, "PM domain %s will not be powered off\n",
152 * Get the generic PM domain for a particular struct device.
153 * This validates the struct device pointer, the PM domain pointer,
154 * and checks that the PM domain pointer is a real generic PM domain.
155 * Any failure results in NULL being returned.
157 static struct generic_pm_domain *genpd_lookup_dev(struct device *dev)
159 struct generic_pm_domain *genpd = NULL, *gpd;
161 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
164 mutex_lock(&gpd_list_lock);
165 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
166 if (&gpd->domain == dev->pm_domain) {
171 mutex_unlock(&gpd_list_lock);
177 * This should only be used where we are certain that the pm_domain
178 * attached to the device is a genpd domain.
180 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
182 if (IS_ERR_OR_NULL(dev->pm_domain))
183 return ERR_PTR(-EINVAL);
185 return pd_to_genpd(dev->pm_domain);
188 static int genpd_stop_dev(const struct generic_pm_domain *genpd,
191 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
194 static int genpd_start_dev(const struct generic_pm_domain *genpd,
197 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
200 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
204 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
205 ret = !!atomic_dec_and_test(&genpd->sd_count);
210 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
212 atomic_inc(&genpd->sd_count);
213 smp_mb__after_atomic();
216 #ifdef CONFIG_DEBUG_FS
217 static void genpd_update_accounting(struct generic_pm_domain *genpd)
222 delta = ktime_sub(now, genpd->accounting_time);
225 * If genpd->status is active, it means we are just
226 * out of off and so update the idle time and vice
229 if (genpd->status == GPD_STATE_ACTIVE) {
230 int state_idx = genpd->state_idx;
232 genpd->states[state_idx].idle_time =
233 ktime_add(genpd->states[state_idx].idle_time, delta);
235 genpd->on_time = ktime_add(genpd->on_time, delta);
238 genpd->accounting_time = now;
241 static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
244 static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd,
247 struct generic_pm_domain_data *pd_data;
248 struct pm_domain_data *pdd;
249 struct gpd_link *link;
251 /* New requested state is same as Max requested state */
252 if (state == genpd->performance_state)
255 /* New requested state is higher than Max requested state */
256 if (state > genpd->performance_state)
259 /* Traverse all devices within the domain */
260 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
261 pd_data = to_gpd_data(pdd);
263 if (pd_data->performance_state > state)
264 state = pd_data->performance_state;
268 * Traverse all sub-domains within the domain. This can be
269 * done without any additional locking as the link->performance_state
270 * field is protected by the master genpd->lock, which is already taken.
272 * Also note that link->performance_state (subdomain's performance state
273 * requirement to master domain) is different from
274 * link->slave->performance_state (current performance state requirement
275 * of the devices/sub-domains of the subdomain) and so can have a
278 * Note that we also take vote from powered-off sub-domains into account
279 * as the same is done for devices right now.
281 list_for_each_entry(link, &genpd->master_links, master_node) {
282 if (link->performance_state > state)
283 state = link->performance_state;
289 static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
290 unsigned int state, int depth)
292 struct generic_pm_domain *master;
293 struct gpd_link *link;
294 int master_state, ret;
296 if (state == genpd->performance_state)
299 /* Propagate to masters of genpd */
300 list_for_each_entry(link, &genpd->slave_links, slave_node) {
301 master = link->master;
303 if (!master->set_performance_state)
306 /* Find master's performance state */
307 ret = dev_pm_opp_xlate_performance_state(genpd->opp_table,
310 if (unlikely(ret < 0))
315 genpd_lock_nested(master, depth + 1);
317 link->prev_performance_state = link->performance_state;
318 link->performance_state = master_state;
319 master_state = _genpd_reeval_performance_state(master,
321 ret = _genpd_set_performance_state(master, master_state, depth + 1);
323 link->performance_state = link->prev_performance_state;
325 genpd_unlock(master);
331 ret = genpd->set_performance_state(genpd, state);
335 genpd->performance_state = state;
339 /* Encountered an error, lets rollback */
340 list_for_each_entry_continue_reverse(link, &genpd->slave_links,
342 master = link->master;
344 if (!master->set_performance_state)
347 genpd_lock_nested(master, depth + 1);
349 master_state = link->prev_performance_state;
350 link->performance_state = master_state;
352 master_state = _genpd_reeval_performance_state(master,
354 if (_genpd_set_performance_state(master, master_state, depth + 1)) {
355 pr_err("%s: Failed to roll back to %d performance state\n",
356 master->name, master_state);
359 genpd_unlock(master);
366 * dev_pm_genpd_set_performance_state- Set performance state of device's power
369 * @dev: Device for which the performance-state needs to be set.
370 * @state: Target performance state of the device. This can be set as 0 when the
371 * device doesn't have any performance state constraints left (And so
372 * the device wouldn't participate anymore to find the target
373 * performance state of the genpd).
375 * It is assumed that the users guarantee that the genpd wouldn't be detached
376 * while this routine is getting called.
378 * Returns 0 on success and negative error values on failures.
380 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
382 struct generic_pm_domain *genpd;
383 struct generic_pm_domain_data *gpd_data;
387 genpd = dev_to_genpd(dev);
391 if (unlikely(!genpd->set_performance_state))
394 if (unlikely(!dev->power.subsys_data ||
395 !dev->power.subsys_data->domain_data)) {
402 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
403 prev = gpd_data->performance_state;
404 gpd_data->performance_state = state;
406 state = _genpd_reeval_performance_state(genpd, state);
407 ret = _genpd_set_performance_state(genpd, state, 0);
409 gpd_data->performance_state = prev;
415 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
417 static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
419 unsigned int state_idx = genpd->state_idx;
424 if (!genpd->power_on)
428 return genpd->power_on(genpd);
430 time_start = ktime_get();
431 ret = genpd->power_on(genpd);
435 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
436 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
439 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
440 genpd->max_off_time_changed = true;
441 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
442 genpd->name, "on", elapsed_ns);
447 static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
449 unsigned int state_idx = genpd->state_idx;
454 if (!genpd->power_off)
458 return genpd->power_off(genpd);
460 time_start = ktime_get();
461 ret = genpd->power_off(genpd);
465 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
466 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
469 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
470 genpd->max_off_time_changed = true;
471 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
472 genpd->name, "off", elapsed_ns);
478 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
479 * @genpd: PM domain to power off.
481 * Queue up the execution of genpd_power_off() unless it's already been done
484 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
486 queue_work(pm_wq, &genpd->power_off_work);
490 * genpd_power_off - Remove power from a given PM domain.
491 * @genpd: PM domain to power down.
492 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
493 * RPM status of the releated device is in an intermediate state, not yet turned
494 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
495 * be RPM_SUSPENDED, while it tries to power off the PM domain.
497 * If all of the @genpd's devices have been suspended and all of its subdomains
498 * have been powered down, remove power from @genpd.
500 static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
503 struct pm_domain_data *pdd;
504 struct gpd_link *link;
505 unsigned int not_suspended = 0;
508 * Do not try to power off the domain in the following situations:
509 * (1) The domain is already in the "power off" state.
510 * (2) System suspend is in progress.
512 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
516 * Abort power off for the PM domain in the following situations:
517 * (1) The domain is configured as always on.
518 * (2) When the domain has a subdomain being powered on.
520 if (genpd_is_always_on(genpd) || atomic_read(&genpd->sd_count) > 0)
523 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
524 enum pm_qos_flags_status stat;
526 stat = dev_pm_qos_flags(pdd->dev, PM_QOS_FLAG_NO_POWER_OFF);
527 if (stat > PM_QOS_FLAGS_NONE)
531 * Do not allow PM domain to be powered off, when an IRQ safe
532 * device is part of a non-IRQ safe domain.
534 if (!pm_runtime_suspended(pdd->dev) ||
535 irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
539 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
542 if (genpd->gov && genpd->gov->power_down_ok) {
543 if (!genpd->gov->power_down_ok(&genpd->domain))
547 /* Default to shallowest state. */
549 genpd->state_idx = 0;
551 if (genpd->power_off) {
554 if (atomic_read(&genpd->sd_count) > 0)
558 * If sd_count > 0 at this point, one of the subdomains hasn't
559 * managed to call genpd_power_on() for the master yet after
560 * incrementing it. In that case genpd_power_on() will wait
561 * for us to drop the lock, so we can call .power_off() and let
562 * the genpd_power_on() restore power for us (this shouldn't
563 * happen very often).
565 ret = _genpd_power_off(genpd, true);
570 genpd->status = GPD_STATE_POWER_OFF;
571 genpd_update_accounting(genpd);
573 list_for_each_entry(link, &genpd->slave_links, slave_node) {
574 genpd_sd_counter_dec(link->master);
575 genpd_lock_nested(link->master, depth + 1);
576 genpd_power_off(link->master, false, depth + 1);
577 genpd_unlock(link->master);
584 * genpd_power_on - Restore power to a given PM domain and its masters.
585 * @genpd: PM domain to power up.
586 * @depth: nesting count for lockdep.
588 * Restore power to @genpd and all of its masters so that it is possible to
589 * resume a device belonging to it.
591 static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
593 struct gpd_link *link;
596 if (genpd_status_on(genpd))
600 * The list is guaranteed not to change while the loop below is being
601 * executed, unless one of the masters' .power_on() callbacks fiddles
604 list_for_each_entry(link, &genpd->slave_links, slave_node) {
605 struct generic_pm_domain *master = link->master;
607 genpd_sd_counter_inc(master);
609 genpd_lock_nested(master, depth + 1);
610 ret = genpd_power_on(master, depth + 1);
611 genpd_unlock(master);
614 genpd_sd_counter_dec(master);
619 ret = _genpd_power_on(genpd, true);
623 genpd->status = GPD_STATE_ACTIVE;
624 genpd_update_accounting(genpd);
629 list_for_each_entry_continue_reverse(link,
632 genpd_sd_counter_dec(link->master);
633 genpd_lock_nested(link->master, depth + 1);
634 genpd_power_off(link->master, false, depth + 1);
635 genpd_unlock(link->master);
641 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
642 unsigned long val, void *ptr)
644 struct generic_pm_domain_data *gpd_data;
647 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
648 dev = gpd_data->base.dev;
651 struct generic_pm_domain *genpd;
652 struct pm_domain_data *pdd;
654 spin_lock_irq(&dev->power.lock);
656 pdd = dev->power.subsys_data ?
657 dev->power.subsys_data->domain_data : NULL;
659 to_gpd_data(pdd)->td.constraint_changed = true;
660 genpd = dev_to_genpd(dev);
662 genpd = ERR_PTR(-ENODATA);
665 spin_unlock_irq(&dev->power.lock);
667 if (!IS_ERR(genpd)) {
669 genpd->max_off_time_changed = true;
674 if (!dev || dev->power.ignore_children)
682 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
683 * @work: Work structure used for scheduling the execution of this function.
685 static void genpd_power_off_work_fn(struct work_struct *work)
687 struct generic_pm_domain *genpd;
689 genpd = container_of(work, struct generic_pm_domain, power_off_work);
692 genpd_power_off(genpd, false, 0);
697 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
698 * @dev: Device to handle.
700 static int __genpd_runtime_suspend(struct device *dev)
702 int (*cb)(struct device *__dev);
704 if (dev->type && dev->type->pm)
705 cb = dev->type->pm->runtime_suspend;
706 else if (dev->class && dev->class->pm)
707 cb = dev->class->pm->runtime_suspend;
708 else if (dev->bus && dev->bus->pm)
709 cb = dev->bus->pm->runtime_suspend;
713 if (!cb && dev->driver && dev->driver->pm)
714 cb = dev->driver->pm->runtime_suspend;
716 return cb ? cb(dev) : 0;
720 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
721 * @dev: Device to handle.
723 static int __genpd_runtime_resume(struct device *dev)
725 int (*cb)(struct device *__dev);
727 if (dev->type && dev->type->pm)
728 cb = dev->type->pm->runtime_resume;
729 else if (dev->class && dev->class->pm)
730 cb = dev->class->pm->runtime_resume;
731 else if (dev->bus && dev->bus->pm)
732 cb = dev->bus->pm->runtime_resume;
736 if (!cb && dev->driver && dev->driver->pm)
737 cb = dev->driver->pm->runtime_resume;
739 return cb ? cb(dev) : 0;
743 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
744 * @dev: Device to suspend.
746 * Carry out a runtime suspend of a device under the assumption that its
747 * pm_domain field points to the domain member of an object of type
748 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
750 static int genpd_runtime_suspend(struct device *dev)
752 struct generic_pm_domain *genpd;
753 bool (*suspend_ok)(struct device *__dev);
754 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
755 bool runtime_pm = pm_runtime_enabled(dev);
760 dev_dbg(dev, "%s()\n", __func__);
762 genpd = dev_to_genpd(dev);
767 * A runtime PM centric subsystem/driver may re-use the runtime PM
768 * callbacks for other purposes than runtime PM. In those scenarios
769 * runtime PM is disabled. Under these circumstances, we shall skip
770 * validating/measuring the PM QoS latency.
772 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
773 if (runtime_pm && suspend_ok && !suspend_ok(dev))
776 /* Measure suspend latency. */
779 time_start = ktime_get();
781 ret = __genpd_runtime_suspend(dev);
785 ret = genpd_stop_dev(genpd, dev);
787 __genpd_runtime_resume(dev);
791 /* Update suspend latency value if the measured time exceeds it. */
793 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
794 if (elapsed_ns > td->suspend_latency_ns) {
795 td->suspend_latency_ns = elapsed_ns;
796 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
798 genpd->max_off_time_changed = true;
799 td->constraint_changed = true;
804 * If power.irq_safe is set, this routine may be run with
805 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
807 if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
811 genpd_power_off(genpd, true, 0);
818 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
819 * @dev: Device to resume.
821 * Carry out a runtime resume of a device under the assumption that its
822 * pm_domain field points to the domain member of an object of type
823 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
825 static int genpd_runtime_resume(struct device *dev)
827 struct generic_pm_domain *genpd;
828 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
829 bool runtime_pm = pm_runtime_enabled(dev);
835 dev_dbg(dev, "%s()\n", __func__);
837 genpd = dev_to_genpd(dev);
842 * As we don't power off a non IRQ safe domain, which holds
843 * an IRQ safe device, we don't need to restore power to it.
845 if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
851 ret = genpd_power_on(genpd, 0);
858 /* Measure resume latency. */
860 if (timed && runtime_pm)
861 time_start = ktime_get();
863 ret = genpd_start_dev(genpd, dev);
867 ret = __genpd_runtime_resume(dev);
871 /* Update resume latency value if the measured time exceeds it. */
872 if (timed && runtime_pm) {
873 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
874 if (elapsed_ns > td->resume_latency_ns) {
875 td->resume_latency_ns = elapsed_ns;
876 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
878 genpd->max_off_time_changed = true;
879 td->constraint_changed = true;
886 genpd_stop_dev(genpd, dev);
888 if (!pm_runtime_is_irq_safe(dev) ||
889 (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
891 genpd_power_off(genpd, true, 0);
898 static bool pd_ignore_unused;
899 static int __init pd_ignore_unused_setup(char *__unused)
901 pd_ignore_unused = true;
904 __setup("pd_ignore_unused", pd_ignore_unused_setup);
907 * genpd_power_off_unused - Power off all PM domains with no devices in use.
909 static int __init genpd_power_off_unused(void)
911 struct generic_pm_domain *genpd;
913 if (pd_ignore_unused) {
914 pr_warn("genpd: Not disabling unused power domains\n");
918 mutex_lock(&gpd_list_lock);
920 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
921 genpd_queue_power_off_work(genpd);
923 mutex_unlock(&gpd_list_lock);
927 late_initcall(genpd_power_off_unused);
929 #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
931 static bool genpd_present(const struct generic_pm_domain *genpd)
933 const struct generic_pm_domain *gpd;
935 if (IS_ERR_OR_NULL(genpd))
938 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
947 #ifdef CONFIG_PM_SLEEP
950 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
951 * @genpd: PM domain to power off, if possible.
952 * @use_lock: use the lock.
953 * @depth: nesting count for lockdep.
955 * Check if the given PM domain can be powered off (during system suspend or
956 * hibernation) and do that if so. Also, in that case propagate to its masters.
958 * This function is only called in "noirq" and "syscore" stages of system power
959 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
960 * these cases the lock must be held.
962 static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
965 struct gpd_link *link;
967 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
970 if (genpd->suspended_count != genpd->device_count
971 || atomic_read(&genpd->sd_count) > 0)
974 /* Choose the deepest state when suspending */
975 genpd->state_idx = genpd->state_count - 1;
976 if (_genpd_power_off(genpd, false))
979 genpd->status = GPD_STATE_POWER_OFF;
981 list_for_each_entry(link, &genpd->slave_links, slave_node) {
982 genpd_sd_counter_dec(link->master);
985 genpd_lock_nested(link->master, depth + 1);
987 genpd_sync_power_off(link->master, use_lock, depth + 1);
990 genpd_unlock(link->master);
995 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
996 * @genpd: PM domain to power on.
997 * @use_lock: use the lock.
998 * @depth: nesting count for lockdep.
1000 * This function is only called in "noirq" and "syscore" stages of system power
1001 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
1002 * these cases the lock must be held.
1004 static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
1007 struct gpd_link *link;
1009 if (genpd_status_on(genpd))
1012 list_for_each_entry(link, &genpd->slave_links, slave_node) {
1013 genpd_sd_counter_inc(link->master);
1016 genpd_lock_nested(link->master, depth + 1);
1018 genpd_sync_power_on(link->master, use_lock, depth + 1);
1021 genpd_unlock(link->master);
1024 _genpd_power_on(genpd, false);
1026 genpd->status = GPD_STATE_ACTIVE;
1030 * resume_needed - Check whether to resume a device before system suspend.
1031 * @dev: Device to check.
1032 * @genpd: PM domain the device belongs to.
1034 * There are two cases in which a device that can wake up the system from sleep
1035 * states should be resumed by genpd_prepare(): (1) if the device is enabled
1036 * to wake up the system and it has to remain active for this purpose while the
1037 * system is in the sleep state and (2) if the device is not enabled to wake up
1038 * the system from sleep states and it generally doesn't generate wakeup signals
1039 * by itself (those signals are generated on its behalf by other parts of the
1040 * system). In the latter case it may be necessary to reconfigure the device's
1041 * wakeup settings during system suspend, because it may have been set up to
1042 * signal remote wakeup from the system's working state as needed by runtime PM.
1043 * Return 'true' in either of the above cases.
1045 static bool resume_needed(struct device *dev,
1046 const struct generic_pm_domain *genpd)
1050 if (!device_can_wakeup(dev))
1053 active_wakeup = genpd_is_active_wakeup(genpd);
1054 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
1058 * genpd_prepare - Start power transition of a device in a PM domain.
1059 * @dev: Device to start the transition of.
1061 * Start a power transition of a device (during a system-wide power transition)
1062 * under the assumption that its pm_domain field points to the domain member of
1063 * an object of type struct generic_pm_domain representing a PM domain
1064 * consisting of I/O devices.
1066 static int genpd_prepare(struct device *dev)
1068 struct generic_pm_domain *genpd;
1071 dev_dbg(dev, "%s()\n", __func__);
1073 genpd = dev_to_genpd(dev);
1078 * If a wakeup request is pending for the device, it should be woken up
1079 * at this point and a system wakeup event should be reported if it's
1080 * set up to wake up the system from sleep states.
1082 if (resume_needed(dev, genpd))
1083 pm_runtime_resume(dev);
1087 if (genpd->prepared_count++ == 0)
1088 genpd->suspended_count = 0;
1090 genpd_unlock(genpd);
1092 ret = pm_generic_prepare(dev);
1096 genpd->prepared_count--;
1098 genpd_unlock(genpd);
1101 /* Never return 1, as genpd don't cope with the direct_complete path. */
1102 return ret >= 0 ? 0 : ret;
1106 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1108 * @dev: Device to suspend.
1109 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
1111 * Stop the device and remove power from the domain if all devices in it have
1114 static int genpd_finish_suspend(struct device *dev, bool poweroff)
1116 struct generic_pm_domain *genpd;
1119 genpd = dev_to_genpd(dev);
1124 ret = pm_generic_poweroff_noirq(dev);
1126 ret = pm_generic_suspend_noirq(dev);
1130 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1133 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1134 !pm_runtime_status_suspended(dev)) {
1135 ret = genpd_stop_dev(genpd, dev);
1138 pm_generic_restore_noirq(dev);
1140 pm_generic_resume_noirq(dev);
1146 genpd->suspended_count++;
1147 genpd_sync_power_off(genpd, true, 0);
1148 genpd_unlock(genpd);
1154 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1155 * @dev: Device to suspend.
1157 * Stop the device and remove power from the domain if all devices in it have
1160 static int genpd_suspend_noirq(struct device *dev)
1162 dev_dbg(dev, "%s()\n", __func__);
1164 return genpd_finish_suspend(dev, false);
1168 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1169 * @dev: Device to resume.
1171 * Restore power to the device's PM domain, if necessary, and start the device.
1173 static int genpd_resume_noirq(struct device *dev)
1175 struct generic_pm_domain *genpd;
1178 dev_dbg(dev, "%s()\n", __func__);
1180 genpd = dev_to_genpd(dev);
1184 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1185 return pm_generic_resume_noirq(dev);
1188 genpd_sync_power_on(genpd, true, 0);
1189 genpd->suspended_count--;
1190 genpd_unlock(genpd);
1192 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1193 !pm_runtime_status_suspended(dev)) {
1194 ret = genpd_start_dev(genpd, dev);
1199 return pm_generic_resume_noirq(dev);
1203 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1204 * @dev: Device to freeze.
1206 * Carry out a late freeze of a device under the assumption that its
1207 * pm_domain field points to the domain member of an object of type
1208 * struct generic_pm_domain representing a power domain consisting of I/O
1211 static int genpd_freeze_noirq(struct device *dev)
1213 const struct generic_pm_domain *genpd;
1216 dev_dbg(dev, "%s()\n", __func__);
1218 genpd = dev_to_genpd(dev);
1222 ret = pm_generic_freeze_noirq(dev);
1226 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1227 !pm_runtime_status_suspended(dev))
1228 ret = genpd_stop_dev(genpd, dev);
1234 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1235 * @dev: Device to thaw.
1237 * Start the device, unless power has been removed from the domain already
1238 * before the system transition.
1240 static int genpd_thaw_noirq(struct device *dev)
1242 const struct generic_pm_domain *genpd;
1245 dev_dbg(dev, "%s()\n", __func__);
1247 genpd = dev_to_genpd(dev);
1251 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1252 !pm_runtime_status_suspended(dev)) {
1253 ret = genpd_start_dev(genpd, dev);
1258 return pm_generic_thaw_noirq(dev);
1262 * genpd_poweroff_noirq - Completion of hibernation of device in an
1264 * @dev: Device to poweroff.
1266 * Stop the device and remove power from the domain if all devices in it have
1269 static int genpd_poweroff_noirq(struct device *dev)
1271 dev_dbg(dev, "%s()\n", __func__);
1273 return genpd_finish_suspend(dev, true);
1277 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1278 * @dev: Device to resume.
1280 * Make sure the domain will be in the same power state as before the
1281 * hibernation the system is resuming from and start the device if necessary.
1283 static int genpd_restore_noirq(struct device *dev)
1285 struct generic_pm_domain *genpd;
1288 dev_dbg(dev, "%s()\n", __func__);
1290 genpd = dev_to_genpd(dev);
1295 * At this point suspended_count == 0 means we are being run for the
1296 * first time for the given domain in the present cycle.
1299 if (genpd->suspended_count++ == 0)
1301 * The boot kernel might put the domain into arbitrary state,
1302 * so make it appear as powered off to genpd_sync_power_on(),
1303 * so that it tries to power it on in case it was really off.
1305 genpd->status = GPD_STATE_POWER_OFF;
1307 genpd_sync_power_on(genpd, true, 0);
1308 genpd_unlock(genpd);
1310 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1311 !pm_runtime_status_suspended(dev)) {
1312 ret = genpd_start_dev(genpd, dev);
1317 return pm_generic_restore_noirq(dev);
1321 * genpd_complete - Complete power transition of a device in a power domain.
1322 * @dev: Device to complete the transition of.
1324 * Complete a power transition of a device (during a system-wide power
1325 * transition) under the assumption that its pm_domain field points to the
1326 * domain member of an object of type struct generic_pm_domain representing
1327 * a power domain consisting of I/O devices.
1329 static void genpd_complete(struct device *dev)
1331 struct generic_pm_domain *genpd;
1333 dev_dbg(dev, "%s()\n", __func__);
1335 genpd = dev_to_genpd(dev);
1339 pm_generic_complete(dev);
1343 genpd->prepared_count--;
1344 if (!genpd->prepared_count)
1345 genpd_queue_power_off_work(genpd);
1347 genpd_unlock(genpd);
1351 * genpd_syscore_switch - Switch power during system core suspend or resume.
1352 * @dev: Device that normally is marked as "always on" to switch power for.
1354 * This routine may only be called during the system core (syscore) suspend or
1355 * resume phase for devices whose "always on" flags are set.
1357 static void genpd_syscore_switch(struct device *dev, bool suspend)
1359 struct generic_pm_domain *genpd;
1361 genpd = dev_to_genpd(dev);
1362 if (!genpd_present(genpd))
1366 genpd->suspended_count++;
1367 genpd_sync_power_off(genpd, false, 0);
1369 genpd_sync_power_on(genpd, false, 0);
1370 genpd->suspended_count--;
1374 void pm_genpd_syscore_poweroff(struct device *dev)
1376 genpd_syscore_switch(dev, true);
1378 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1380 void pm_genpd_syscore_poweron(struct device *dev)
1382 genpd_syscore_switch(dev, false);
1384 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1386 #else /* !CONFIG_PM_SLEEP */
1388 #define genpd_prepare NULL
1389 #define genpd_suspend_noirq NULL
1390 #define genpd_resume_noirq NULL
1391 #define genpd_freeze_noirq NULL
1392 #define genpd_thaw_noirq NULL
1393 #define genpd_poweroff_noirq NULL
1394 #define genpd_restore_noirq NULL
1395 #define genpd_complete NULL
1397 #endif /* CONFIG_PM_SLEEP */
1399 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1400 struct gpd_timing_data *td)
1402 struct generic_pm_domain_data *gpd_data;
1405 ret = dev_pm_get_subsys_data(dev);
1407 return ERR_PTR(ret);
1409 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1418 gpd_data->base.dev = dev;
1419 gpd_data->td.constraint_changed = true;
1420 gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
1421 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1423 spin_lock_irq(&dev->power.lock);
1425 if (dev->power.subsys_data->domain_data) {
1430 dev->power.subsys_data->domain_data = &gpd_data->base;
1432 spin_unlock_irq(&dev->power.lock);
1437 spin_unlock_irq(&dev->power.lock);
1440 dev_pm_put_subsys_data(dev);
1441 return ERR_PTR(ret);
1444 static void genpd_free_dev_data(struct device *dev,
1445 struct generic_pm_domain_data *gpd_data)
1447 spin_lock_irq(&dev->power.lock);
1449 dev->power.subsys_data->domain_data = NULL;
1451 spin_unlock_irq(&dev->power.lock);
1454 dev_pm_put_subsys_data(dev);
1457 static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1458 struct gpd_timing_data *td)
1460 struct generic_pm_domain_data *gpd_data;
1463 dev_dbg(dev, "%s()\n", __func__);
1465 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1468 gpd_data = genpd_alloc_dev_data(dev, td);
1469 if (IS_ERR(gpd_data))
1470 return PTR_ERR(gpd_data);
1474 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1478 dev_pm_domain_set(dev, &genpd->domain);
1480 genpd->device_count++;
1481 genpd->max_off_time_changed = true;
1483 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1486 genpd_unlock(genpd);
1489 genpd_free_dev_data(dev, gpd_data);
1491 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1497 * pm_genpd_add_device - Add a device to an I/O PM domain.
1498 * @genpd: PM domain to add the device to.
1499 * @dev: Device to be added.
1501 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1505 mutex_lock(&gpd_list_lock);
1506 ret = genpd_add_device(genpd, dev, NULL);
1507 mutex_unlock(&gpd_list_lock);
1511 EXPORT_SYMBOL_GPL(pm_genpd_add_device);
1513 static int genpd_remove_device(struct generic_pm_domain *genpd,
1516 struct generic_pm_domain_data *gpd_data;
1517 struct pm_domain_data *pdd;
1520 dev_dbg(dev, "%s()\n", __func__);
1522 pdd = dev->power.subsys_data->domain_data;
1523 gpd_data = to_gpd_data(pdd);
1524 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1528 if (genpd->prepared_count > 0) {
1533 genpd->device_count--;
1534 genpd->max_off_time_changed = true;
1536 if (genpd->detach_dev)
1537 genpd->detach_dev(genpd, dev);
1539 dev_pm_domain_set(dev, NULL);
1541 list_del_init(&pdd->list_node);
1543 genpd_unlock(genpd);
1545 genpd_free_dev_data(dev, gpd_data);
1550 genpd_unlock(genpd);
1551 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1557 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1558 * @dev: Device to be removed.
1560 int pm_genpd_remove_device(struct device *dev)
1562 struct generic_pm_domain *genpd = genpd_lookup_dev(dev);
1567 return genpd_remove_device(genpd, dev);
1569 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1571 static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1572 struct generic_pm_domain *subdomain)
1574 struct gpd_link *link, *itr;
1577 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1578 || genpd == subdomain)
1582 * If the domain can be powered on/off in an IRQ safe
1583 * context, ensure that the subdomain can also be
1584 * powered on/off in that context.
1586 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1587 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1588 genpd->name, subdomain->name);
1592 link = kzalloc(sizeof(*link), GFP_KERNEL);
1596 genpd_lock(subdomain);
1597 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1599 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1604 list_for_each_entry(itr, &genpd->master_links, master_node) {
1605 if (itr->slave == subdomain && itr->master == genpd) {
1611 link->master = genpd;
1612 list_add_tail(&link->master_node, &genpd->master_links);
1613 link->slave = subdomain;
1614 list_add_tail(&link->slave_node, &subdomain->slave_links);
1615 if (genpd_status_on(subdomain))
1616 genpd_sd_counter_inc(genpd);
1619 genpd_unlock(genpd);
1620 genpd_unlock(subdomain);
1627 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1628 * @genpd: Master PM domain to add the subdomain to.
1629 * @subdomain: Subdomain to be added.
1631 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1632 struct generic_pm_domain *subdomain)
1636 mutex_lock(&gpd_list_lock);
1637 ret = genpd_add_subdomain(genpd, subdomain);
1638 mutex_unlock(&gpd_list_lock);
1642 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1645 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1646 * @genpd: Master PM domain to remove the subdomain from.
1647 * @subdomain: Subdomain to be removed.
1649 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1650 struct generic_pm_domain *subdomain)
1652 struct gpd_link *l, *link;
1655 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1658 genpd_lock(subdomain);
1659 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1661 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1662 pr_warn("%s: unable to remove subdomain %s\n",
1663 genpd->name, subdomain->name);
1668 list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
1669 if (link->slave != subdomain)
1672 list_del(&link->master_node);
1673 list_del(&link->slave_node);
1675 if (genpd_status_on(subdomain))
1676 genpd_sd_counter_dec(genpd);
1683 genpd_unlock(genpd);
1684 genpd_unlock(subdomain);
1688 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1690 static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1692 struct genpd_power_state *state;
1694 state = kzalloc(sizeof(*state), GFP_KERNEL);
1698 genpd->states = state;
1699 genpd->state_count = 1;
1700 genpd->free = state;
1705 static void genpd_lock_init(struct generic_pm_domain *genpd)
1707 if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1708 spin_lock_init(&genpd->slock);
1709 genpd->lock_ops = &genpd_spin_ops;
1711 mutex_init(&genpd->mlock);
1712 genpd->lock_ops = &genpd_mtx_ops;
1717 * pm_genpd_init - Initialize a generic I/O PM domain object.
1718 * @genpd: PM domain object to initialize.
1719 * @gov: PM domain governor to associate with the domain (may be NULL).
1720 * @is_off: Initial value of the domain's power_is_off field.
1722 * Returns 0 on successful initialization, else a negative error code.
1724 int pm_genpd_init(struct generic_pm_domain *genpd,
1725 struct dev_power_governor *gov, bool is_off)
1729 if (IS_ERR_OR_NULL(genpd))
1732 INIT_LIST_HEAD(&genpd->master_links);
1733 INIT_LIST_HEAD(&genpd->slave_links);
1734 INIT_LIST_HEAD(&genpd->dev_list);
1735 genpd_lock_init(genpd);
1737 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1738 atomic_set(&genpd->sd_count, 0);
1739 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1740 genpd->device_count = 0;
1741 genpd->max_off_time_ns = -1;
1742 genpd->max_off_time_changed = true;
1743 genpd->provider = NULL;
1744 genpd->has_provider = false;
1745 genpd->accounting_time = ktime_get();
1746 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1747 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
1748 genpd->domain.ops.prepare = genpd_prepare;
1749 genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
1750 genpd->domain.ops.resume_noirq = genpd_resume_noirq;
1751 genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
1752 genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
1753 genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
1754 genpd->domain.ops.restore_noirq = genpd_restore_noirq;
1755 genpd->domain.ops.complete = genpd_complete;
1757 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1758 genpd->dev_ops.stop = pm_clk_suspend;
1759 genpd->dev_ops.start = pm_clk_resume;
1762 /* Always-on domains must be powered on at initialization. */
1763 if (genpd_is_always_on(genpd) && !genpd_status_on(genpd))
1766 /* Use only one "off" state if there were no states declared */
1767 if (genpd->state_count == 0) {
1768 ret = genpd_set_default_power_state(genpd);
1771 } else if (!gov && genpd->state_count > 1) {
1772 pr_warn("%s: no governor for states\n", genpd->name);
1775 device_initialize(&genpd->dev);
1776 dev_set_name(&genpd->dev, "%s", genpd->name);
1778 mutex_lock(&gpd_list_lock);
1779 list_add(&genpd->gpd_list_node, &gpd_list);
1780 mutex_unlock(&gpd_list_lock);
1784 EXPORT_SYMBOL_GPL(pm_genpd_init);
1786 static int genpd_remove(struct generic_pm_domain *genpd)
1788 struct gpd_link *l, *link;
1790 if (IS_ERR_OR_NULL(genpd))
1795 if (genpd->has_provider) {
1796 genpd_unlock(genpd);
1797 pr_err("Provider present, unable to remove %s\n", genpd->name);
1801 if (!list_empty(&genpd->master_links) || genpd->device_count) {
1802 genpd_unlock(genpd);
1803 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1807 list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
1808 list_del(&link->master_node);
1809 list_del(&link->slave_node);
1813 list_del(&genpd->gpd_list_node);
1814 genpd_unlock(genpd);
1815 cancel_work_sync(&genpd->power_off_work);
1817 pr_debug("%s: removed %s\n", __func__, genpd->name);
1823 * pm_genpd_remove - Remove a generic I/O PM domain
1824 * @genpd: Pointer to PM domain that is to be removed.
1826 * To remove the PM domain, this function:
1827 * - Removes the PM domain as a subdomain to any parent domains,
1829 * - Removes the PM domain from the list of registered PM domains.
1831 * The PM domain will only be removed, if the associated provider has
1832 * been removed, it is not a parent to any other PM domain and has no
1833 * devices associated with it.
1835 int pm_genpd_remove(struct generic_pm_domain *genpd)
1839 mutex_lock(&gpd_list_lock);
1840 ret = genpd_remove(genpd);
1841 mutex_unlock(&gpd_list_lock);
1845 EXPORT_SYMBOL_GPL(pm_genpd_remove);
1847 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1850 * Device Tree based PM domain providers.
1852 * The code below implements generic device tree based PM domain providers that
1853 * bind device tree nodes with generic PM domains registered in the system.
1855 * Any driver that registers generic PM domains and needs to support binding of
1856 * devices to these domains is supposed to register a PM domain provider, which
1857 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1859 * Two simple mapping functions have been provided for convenience:
1860 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1861 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1866 * struct of_genpd_provider - PM domain provider registration structure
1867 * @link: Entry in global list of PM domain providers
1868 * @node: Pointer to device tree node of PM domain provider
1869 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1871 * @data: context pointer to be passed into @xlate callback
1873 struct of_genpd_provider {
1874 struct list_head link;
1875 struct device_node *node;
1876 genpd_xlate_t xlate;
1880 /* List of registered PM domain providers. */
1881 static LIST_HEAD(of_genpd_providers);
1882 /* Mutex to protect the list above. */
1883 static DEFINE_MUTEX(of_genpd_mutex);
1886 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1887 * @genpdspec: OF phandle args to map into a PM domain
1888 * @data: xlate function private data - pointer to struct generic_pm_domain
1890 * This is a generic xlate function that can be used to model PM domains that
1891 * have their own device tree nodes. The private data of xlate function needs
1892 * to be a valid pointer to struct generic_pm_domain.
1894 static struct generic_pm_domain *genpd_xlate_simple(
1895 struct of_phandle_args *genpdspec,
1902 * genpd_xlate_onecell() - Xlate function using a single index.
1903 * @genpdspec: OF phandle args to map into a PM domain
1904 * @data: xlate function private data - pointer to struct genpd_onecell_data
1906 * This is a generic xlate function that can be used to model simple PM domain
1907 * controllers that have one device tree node and provide multiple PM domains.
1908 * A single cell is used as an index into an array of PM domains specified in
1909 * the genpd_onecell_data struct when registering the provider.
1911 static struct generic_pm_domain *genpd_xlate_onecell(
1912 struct of_phandle_args *genpdspec,
1915 struct genpd_onecell_data *genpd_data = data;
1916 unsigned int idx = genpdspec->args[0];
1918 if (genpdspec->args_count != 1)
1919 return ERR_PTR(-EINVAL);
1921 if (idx >= genpd_data->num_domains) {
1922 pr_err("%s: invalid domain index %u\n", __func__, idx);
1923 return ERR_PTR(-EINVAL);
1926 if (!genpd_data->domains[idx])
1927 return ERR_PTR(-ENOENT);
1929 return genpd_data->domains[idx];
1933 * genpd_add_provider() - Register a PM domain provider for a node
1934 * @np: Device node pointer associated with the PM domain provider.
1935 * @xlate: Callback for decoding PM domain from phandle arguments.
1936 * @data: Context pointer for @xlate callback.
1938 static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1941 struct of_genpd_provider *cp;
1943 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1947 cp->node = of_node_get(np);
1951 mutex_lock(&of_genpd_mutex);
1952 list_add(&cp->link, &of_genpd_providers);
1953 mutex_unlock(&of_genpd_mutex);
1954 pr_debug("Added domain provider from %pOF\n", np);
1960 * of_genpd_add_provider_simple() - Register a simple PM domain provider
1961 * @np: Device node pointer associated with the PM domain provider.
1962 * @genpd: Pointer to PM domain associated with the PM domain provider.
1964 int of_genpd_add_provider_simple(struct device_node *np,
1965 struct generic_pm_domain *genpd)
1972 mutex_lock(&gpd_list_lock);
1974 if (!genpd_present(genpd))
1977 genpd->dev.of_node = np;
1979 /* Parse genpd OPP table */
1980 if (genpd->set_performance_state) {
1981 ret = dev_pm_opp_of_add_table(&genpd->dev);
1983 dev_err(&genpd->dev, "Failed to add OPP table: %d\n",
1989 * Save table for faster processing while setting performance
1992 genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
1993 WARN_ON(!genpd->opp_table);
1996 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
1998 if (genpd->set_performance_state) {
1999 dev_pm_opp_put_opp_table(genpd->opp_table);
2000 dev_pm_opp_of_remove_table(&genpd->dev);
2006 genpd->provider = &np->fwnode;
2007 genpd->has_provider = true;
2010 mutex_unlock(&gpd_list_lock);
2014 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
2017 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
2018 * @np: Device node pointer associated with the PM domain provider.
2019 * @data: Pointer to the data associated with the PM domain provider.
2021 int of_genpd_add_provider_onecell(struct device_node *np,
2022 struct genpd_onecell_data *data)
2024 struct generic_pm_domain *genpd;
2031 mutex_lock(&gpd_list_lock);
2034 data->xlate = genpd_xlate_onecell;
2036 for (i = 0; i < data->num_domains; i++) {
2037 genpd = data->domains[i];
2041 if (!genpd_present(genpd))
2044 genpd->dev.of_node = np;
2046 /* Parse genpd OPP table */
2047 if (genpd->set_performance_state) {
2048 ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
2050 dev_err(&genpd->dev, "Failed to add OPP table for index %d: %d\n",
2056 * Save table for faster processing while setting
2057 * performance state.
2059 genpd->opp_table = dev_pm_opp_get_opp_table_indexed(&genpd->dev, i);
2060 WARN_ON(!genpd->opp_table);
2063 genpd->provider = &np->fwnode;
2064 genpd->has_provider = true;
2067 ret = genpd_add_provider(np, data->xlate, data);
2071 mutex_unlock(&gpd_list_lock);
2077 genpd = data->domains[i];
2082 genpd->provider = NULL;
2083 genpd->has_provider = false;
2085 if (genpd->set_performance_state) {
2086 dev_pm_opp_put_opp_table(genpd->opp_table);
2087 dev_pm_opp_of_remove_table(&genpd->dev);
2091 mutex_unlock(&gpd_list_lock);
2095 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
2098 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2099 * @np: Device node pointer associated with the PM domain provider
2101 void of_genpd_del_provider(struct device_node *np)
2103 struct of_genpd_provider *cp, *tmp;
2104 struct generic_pm_domain *gpd;
2106 mutex_lock(&gpd_list_lock);
2107 mutex_lock(&of_genpd_mutex);
2108 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
2109 if (cp->node == np) {
2111 * For each PM domain associated with the
2112 * provider, set the 'has_provider' to false
2113 * so that the PM domain can be safely removed.
2115 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2116 if (gpd->provider == &np->fwnode) {
2117 gpd->has_provider = false;
2119 if (!gpd->set_performance_state)
2122 dev_pm_opp_put_opp_table(gpd->opp_table);
2123 dev_pm_opp_of_remove_table(&gpd->dev);
2127 list_del(&cp->link);
2128 of_node_put(cp->node);
2133 mutex_unlock(&of_genpd_mutex);
2134 mutex_unlock(&gpd_list_lock);
2136 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2139 * genpd_get_from_provider() - Look-up PM domain
2140 * @genpdspec: OF phandle args to use for look-up
2142 * Looks for a PM domain provider under the node specified by @genpdspec and if
2143 * found, uses xlate function of the provider to map phandle args to a PM
2146 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2149 static struct generic_pm_domain *genpd_get_from_provider(
2150 struct of_phandle_args *genpdspec)
2152 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2153 struct of_genpd_provider *provider;
2156 return ERR_PTR(-EINVAL);
2158 mutex_lock(&of_genpd_mutex);
2160 /* Check if we have such a provider in our array */
2161 list_for_each_entry(provider, &of_genpd_providers, link) {
2162 if (provider->node == genpdspec->np)
2163 genpd = provider->xlate(genpdspec, provider->data);
2168 mutex_unlock(&of_genpd_mutex);
2174 * of_genpd_add_device() - Add a device to an I/O PM domain
2175 * @genpdspec: OF phandle args to use for look-up PM domain
2176 * @dev: Device to be added.
2178 * Looks-up an I/O PM domain based upon phandle args provided and adds
2179 * the device to the PM domain. Returns a negative error code on failure.
2181 int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
2183 struct generic_pm_domain *genpd;
2186 mutex_lock(&gpd_list_lock);
2188 genpd = genpd_get_from_provider(genpdspec);
2189 if (IS_ERR(genpd)) {
2190 ret = PTR_ERR(genpd);
2194 ret = genpd_add_device(genpd, dev, NULL);
2197 mutex_unlock(&gpd_list_lock);
2201 EXPORT_SYMBOL_GPL(of_genpd_add_device);
2204 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2205 * @parent_spec: OF phandle args to use for parent PM domain look-up
2206 * @subdomain_spec: OF phandle args to use for subdomain look-up
2208 * Looks-up a parent PM domain and subdomain based upon phandle args
2209 * provided and adds the subdomain to the parent PM domain. Returns a
2210 * negative error code on failure.
2212 int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
2213 struct of_phandle_args *subdomain_spec)
2215 struct generic_pm_domain *parent, *subdomain;
2218 mutex_lock(&gpd_list_lock);
2220 parent = genpd_get_from_provider(parent_spec);
2221 if (IS_ERR(parent)) {
2222 ret = PTR_ERR(parent);
2226 subdomain = genpd_get_from_provider(subdomain_spec);
2227 if (IS_ERR(subdomain)) {
2228 ret = PTR_ERR(subdomain);
2232 ret = genpd_add_subdomain(parent, subdomain);
2235 mutex_unlock(&gpd_list_lock);
2239 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2242 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2243 * @provider: Pointer to device structure associated with provider
2245 * Find the last PM domain that was added by a particular provider and
2246 * remove this PM domain from the list of PM domains. The provider is
2247 * identified by the 'provider' device structure that is passed. The PM
2248 * domain will only be removed, if the provider associated with domain
2251 * Returns a valid pointer to struct generic_pm_domain on success or
2252 * ERR_PTR() on failure.
2254 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2256 struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2259 if (IS_ERR_OR_NULL(np))
2260 return ERR_PTR(-EINVAL);
2262 mutex_lock(&gpd_list_lock);
2263 list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2264 if (gpd->provider == &np->fwnode) {
2265 ret = genpd_remove(gpd);
2266 genpd = ret ? ERR_PTR(ret) : gpd;
2270 mutex_unlock(&gpd_list_lock);
2274 EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2276 static void genpd_release_dev(struct device *dev)
2281 static struct bus_type genpd_bus_type = {
2286 * genpd_dev_pm_detach - Detach a device from its PM domain.
2287 * @dev: Device to detach.
2288 * @power_off: Currently not used
2290 * Try to locate a corresponding generic PM domain, which the device was
2291 * attached to previously. If such is found, the device is detached from it.
2293 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2295 struct generic_pm_domain *pd;
2299 pd = dev_to_genpd(dev);
2303 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2305 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2306 ret = genpd_remove_device(pd, dev);
2315 dev_err(dev, "failed to remove from PM domain %s: %d",
2320 /* Check if PM domain can be powered off after removing this device. */
2321 genpd_queue_power_off_work(pd);
2323 /* Unregister the device if it was created by genpd. */
2324 if (dev->bus == &genpd_bus_type)
2325 device_unregister(dev);
2328 static void genpd_dev_pm_sync(struct device *dev)
2330 struct generic_pm_domain *pd;
2332 pd = dev_to_genpd(dev);
2336 genpd_queue_power_off_work(pd);
2339 static int __genpd_dev_pm_attach(struct device *dev, struct device_node *np,
2340 unsigned int index, bool power_on)
2342 struct of_phandle_args pd_args;
2343 struct generic_pm_domain *pd;
2346 ret = of_parse_phandle_with_args(np, "power-domains",
2347 "#power-domain-cells", index, &pd_args);
2351 mutex_lock(&gpd_list_lock);
2352 pd = genpd_get_from_provider(&pd_args);
2353 of_node_put(pd_args.np);
2355 mutex_unlock(&gpd_list_lock);
2356 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2357 __func__, PTR_ERR(pd));
2358 return driver_deferred_probe_check_state(dev);
2361 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2363 ret = genpd_add_device(pd, dev, NULL);
2364 mutex_unlock(&gpd_list_lock);
2367 if (ret != -EPROBE_DEFER)
2368 dev_err(dev, "failed to add to PM domain %s: %d",
2373 dev->pm_domain->detach = genpd_dev_pm_detach;
2374 dev->pm_domain->sync = genpd_dev_pm_sync;
2378 ret = genpd_power_on(pd, 0);
2383 genpd_remove_device(pd, dev);
2385 return ret ? -EPROBE_DEFER : 1;
2389 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2390 * @dev: Device to attach.
2392 * Parse device's OF node to find a PM domain specifier. If such is found,
2393 * attaches the device to retrieved pm_domain ops.
2395 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2396 * PM domain or when multiple power-domains exists for it, else a negative error
2397 * code. Note that if a power-domain exists for the device, but it cannot be
2398 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2399 * not probed and to re-try again later.
2401 int genpd_dev_pm_attach(struct device *dev)
2407 * Devices with multiple PM domains must be attached separately, as we
2408 * can only attach one PM domain per device.
2410 if (of_count_phandle_with_args(dev->of_node, "power-domains",
2411 "#power-domain-cells") != 1)
2414 return __genpd_dev_pm_attach(dev, dev->of_node, 0, true);
2416 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2419 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2420 * @dev: The device used to lookup the PM domain.
2421 * @index: The index of the PM domain.
2423 * Parse device's OF node to find a PM domain specifier at the provided @index.
2424 * If such is found, creates a virtual device and attaches it to the retrieved
2425 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2426 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2428 * Returns the created virtual device if successfully attached PM domain, NULL
2429 * when the device don't need a PM domain, else an ERR_PTR() in case of
2430 * failures. If a power-domain exists for the device, but cannot be found or
2431 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2432 * is not probed and to re-try again later.
2434 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
2437 struct device *virt_dev;
2444 /* Deal only with devices using multiple PM domains. */
2445 num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
2446 "#power-domain-cells");
2447 if (num_domains < 2 || index >= num_domains)
2450 /* Allocate and register device on the genpd bus. */
2451 virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL);
2453 return ERR_PTR(-ENOMEM);
2455 dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev));
2456 virt_dev->bus = &genpd_bus_type;
2457 virt_dev->release = genpd_release_dev;
2459 ret = device_register(virt_dev);
2462 return ERR_PTR(ret);
2465 /* Try to attach the device to the PM domain at the specified index. */
2466 ret = __genpd_dev_pm_attach(virt_dev, dev->of_node, index, false);
2468 device_unregister(virt_dev);
2469 return ret ? ERR_PTR(ret) : NULL;
2472 pm_runtime_enable(virt_dev);
2473 genpd_queue_power_off_work(dev_to_genpd(virt_dev));
2477 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
2480 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
2481 * @dev: The device used to lookup the PM domain.
2482 * @name: The name of the PM domain.
2484 * Parse device's OF node to find a PM domain specifier using the
2485 * power-domain-names DT property. For further description see
2486 * genpd_dev_pm_attach_by_id().
2488 struct device *genpd_dev_pm_attach_by_name(struct device *dev, const char *name)
2495 index = of_property_match_string(dev->of_node, "power-domain-names",
2500 return genpd_dev_pm_attach_by_id(dev, index);
2503 static const struct of_device_id idle_state_match[] = {
2504 { .compatible = "domain-idle-state", },
2508 static int genpd_parse_state(struct genpd_power_state *genpd_state,
2509 struct device_node *state_node)
2513 u32 entry_latency, exit_latency;
2515 err = of_property_read_u32(state_node, "entry-latency-us",
2518 pr_debug(" * %pOF missing entry-latency-us property\n",
2523 err = of_property_read_u32(state_node, "exit-latency-us",
2526 pr_debug(" * %pOF missing exit-latency-us property\n",
2531 err = of_property_read_u32(state_node, "min-residency-us", &residency);
2533 genpd_state->residency_ns = 1000 * residency;
2535 genpd_state->power_on_latency_ns = 1000 * exit_latency;
2536 genpd_state->power_off_latency_ns = 1000 * entry_latency;
2537 genpd_state->fwnode = &state_node->fwnode;
2542 static int genpd_iterate_idle_states(struct device_node *dn,
2543 struct genpd_power_state *states)
2546 struct of_phandle_iterator it;
2547 struct device_node *np;
2550 ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2554 /* Loop over the phandles until all the requested entry is found */
2555 of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
2557 if (!of_match_node(idle_state_match, np))
2560 ret = genpd_parse_state(&states[i], np);
2562 pr_err("Parsing idle state node %pOF failed with err %d\n",
2575 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2577 * @dn: The genpd device node
2578 * @states: The pointer to which the state array will be saved.
2579 * @n: The count of elements in the array returned from this function.
2581 * Returns the device states parsed from the OF node. The memory for the states
2582 * is allocated by this function and is the responsibility of the caller to
2583 * free the memory after use. If any or zero compatible domain idle states is
2584 * found it returns 0 and in case of errors, a negative error code is returned.
2586 int of_genpd_parse_idle_states(struct device_node *dn,
2587 struct genpd_power_state **states, int *n)
2589 struct genpd_power_state *st;
2592 ret = genpd_iterate_idle_states(dn, NULL);
2602 st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
2606 ret = genpd_iterate_idle_states(dn, st);
2609 return ret < 0 ? ret : -EINVAL;
2617 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2620 * pm_genpd_opp_to_performance_state - Gets performance state of the genpd from its OPP node.
2622 * @genpd_dev: Genpd's device for which the performance-state needs to be found.
2623 * @opp: struct dev_pm_opp of the OPP for which we need to find performance
2626 * Returns performance state encoded in the OPP of the genpd. This calls
2627 * platform specific genpd->opp_to_performance_state() callback to translate
2628 * power domain OPP to performance state.
2630 * Returns performance state on success and 0 on failure.
2632 unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
2633 struct dev_pm_opp *opp)
2635 struct generic_pm_domain *genpd = NULL;
2638 genpd = container_of(genpd_dev, struct generic_pm_domain, dev);
2640 if (unlikely(!genpd->opp_to_performance_state))
2644 state = genpd->opp_to_performance_state(genpd, opp);
2645 genpd_unlock(genpd);
2649 EXPORT_SYMBOL_GPL(pm_genpd_opp_to_performance_state);
2651 static int __init genpd_bus_init(void)
2653 return bus_register(&genpd_bus_type);
2655 core_initcall(genpd_bus_init);
2657 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2660 /*** debugfs support ***/
2662 #ifdef CONFIG_DEBUG_FS
2663 #include <linux/pm.h>
2664 #include <linux/device.h>
2665 #include <linux/debugfs.h>
2666 #include <linux/seq_file.h>
2667 #include <linux/init.h>
2668 #include <linux/kobject.h>
2669 static struct dentry *genpd_debugfs_dir;
2672 * TODO: This function is a slightly modified version of rtpm_status_show
2673 * from sysfs.c, so generalize it.
2675 static void rtpm_status_str(struct seq_file *s, struct device *dev)
2677 static const char * const status_lookup[] = {
2678 [RPM_ACTIVE] = "active",
2679 [RPM_RESUMING] = "resuming",
2680 [RPM_SUSPENDED] = "suspended",
2681 [RPM_SUSPENDING] = "suspending"
2685 if (dev->power.runtime_error)
2687 else if (dev->power.disable_depth)
2689 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2690 p = status_lookup[dev->power.runtime_status];
2697 static int genpd_summary_one(struct seq_file *s,
2698 struct generic_pm_domain *genpd)
2700 static const char * const status_lookup[] = {
2701 [GPD_STATE_ACTIVE] = "on",
2702 [GPD_STATE_POWER_OFF] = "off"
2704 struct pm_domain_data *pm_data;
2705 const char *kobj_path;
2706 struct gpd_link *link;
2710 ret = genpd_lock_interruptible(genpd);
2712 return -ERESTARTSYS;
2714 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2716 if (!genpd_status_on(genpd))
2717 snprintf(state, sizeof(state), "%s-%u",
2718 status_lookup[genpd->status], genpd->state_idx);
2720 snprintf(state, sizeof(state), "%s",
2721 status_lookup[genpd->status]);
2722 seq_printf(s, "%-30s %-15s ", genpd->name, state);
2725 * Modifications on the list require holding locks on both
2726 * master and slave, so we are safe.
2727 * Also genpd->name is immutable.
2729 list_for_each_entry(link, &genpd->master_links, master_node) {
2730 seq_printf(s, "%s", link->slave->name);
2731 if (!list_is_last(&link->master_node, &genpd->master_links))
2735 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2736 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2737 genpd_is_irq_safe(genpd) ?
2738 GFP_ATOMIC : GFP_KERNEL);
2739 if (kobj_path == NULL)
2742 seq_printf(s, "\n %-50s ", kobj_path);
2743 rtpm_status_str(s, pm_data->dev);
2749 genpd_unlock(genpd);
2754 static int summary_show(struct seq_file *s, void *data)
2756 struct generic_pm_domain *genpd;
2759 seq_puts(s, "domain status slaves\n");
2760 seq_puts(s, " /device runtime status\n");
2761 seq_puts(s, "----------------------------------------------------------------------\n");
2763 ret = mutex_lock_interruptible(&gpd_list_lock);
2765 return -ERESTARTSYS;
2767 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2768 ret = genpd_summary_one(s, genpd);
2772 mutex_unlock(&gpd_list_lock);
2777 static int status_show(struct seq_file *s, void *data)
2779 static const char * const status_lookup[] = {
2780 [GPD_STATE_ACTIVE] = "on",
2781 [GPD_STATE_POWER_OFF] = "off"
2784 struct generic_pm_domain *genpd = s->private;
2787 ret = genpd_lock_interruptible(genpd);
2789 return -ERESTARTSYS;
2791 if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
2794 if (genpd->status == GPD_STATE_POWER_OFF)
2795 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
2798 seq_printf(s, "%s\n", status_lookup[genpd->status]);
2800 genpd_unlock(genpd);
2804 static int sub_domains_show(struct seq_file *s, void *data)
2806 struct generic_pm_domain *genpd = s->private;
2807 struct gpd_link *link;
2810 ret = genpd_lock_interruptible(genpd);
2812 return -ERESTARTSYS;
2814 list_for_each_entry(link, &genpd->master_links, master_node)
2815 seq_printf(s, "%s\n", link->slave->name);
2817 genpd_unlock(genpd);
2821 static int idle_states_show(struct seq_file *s, void *data)
2823 struct generic_pm_domain *genpd = s->private;
2827 ret = genpd_lock_interruptible(genpd);
2829 return -ERESTARTSYS;
2831 seq_puts(s, "State Time Spent(ms)\n");
2833 for (i = 0; i < genpd->state_count; i++) {
2837 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2838 (genpd->state_idx == i))
2839 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2841 msecs = ktime_to_ms(
2842 ktime_add(genpd->states[i].idle_time, delta));
2843 seq_printf(s, "S%-13i %lld\n", i, msecs);
2846 genpd_unlock(genpd);
2850 static int active_time_show(struct seq_file *s, void *data)
2852 struct generic_pm_domain *genpd = s->private;
2856 ret = genpd_lock_interruptible(genpd);
2858 return -ERESTARTSYS;
2860 if (genpd->status == GPD_STATE_ACTIVE)
2861 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2863 seq_printf(s, "%lld ms\n", ktime_to_ms(
2864 ktime_add(genpd->on_time, delta)));
2866 genpd_unlock(genpd);
2870 static int total_idle_time_show(struct seq_file *s, void *data)
2872 struct generic_pm_domain *genpd = s->private;
2873 ktime_t delta = 0, total = 0;
2877 ret = genpd_lock_interruptible(genpd);
2879 return -ERESTARTSYS;
2881 for (i = 0; i < genpd->state_count; i++) {
2883 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2884 (genpd->state_idx == i))
2885 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2887 total = ktime_add(total, genpd->states[i].idle_time);
2889 total = ktime_add(total, delta);
2891 seq_printf(s, "%lld ms\n", ktime_to_ms(total));
2893 genpd_unlock(genpd);
2898 static int devices_show(struct seq_file *s, void *data)
2900 struct generic_pm_domain *genpd = s->private;
2901 struct pm_domain_data *pm_data;
2902 const char *kobj_path;
2905 ret = genpd_lock_interruptible(genpd);
2907 return -ERESTARTSYS;
2909 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2910 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2911 genpd_is_irq_safe(genpd) ?
2912 GFP_ATOMIC : GFP_KERNEL);
2913 if (kobj_path == NULL)
2916 seq_printf(s, "%s\n", kobj_path);
2920 genpd_unlock(genpd);
2924 static int perf_state_show(struct seq_file *s, void *data)
2926 struct generic_pm_domain *genpd = s->private;
2928 if (genpd_lock_interruptible(genpd))
2929 return -ERESTARTSYS;
2931 seq_printf(s, "%u\n", genpd->performance_state);
2933 genpd_unlock(genpd);
2937 DEFINE_SHOW_ATTRIBUTE(summary);
2938 DEFINE_SHOW_ATTRIBUTE(status);
2939 DEFINE_SHOW_ATTRIBUTE(sub_domains);
2940 DEFINE_SHOW_ATTRIBUTE(idle_states);
2941 DEFINE_SHOW_ATTRIBUTE(active_time);
2942 DEFINE_SHOW_ATTRIBUTE(total_idle_time);
2943 DEFINE_SHOW_ATTRIBUTE(devices);
2944 DEFINE_SHOW_ATTRIBUTE(perf_state);
2946 static int __init genpd_debug_init(void)
2949 struct generic_pm_domain *genpd;
2951 genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2953 debugfs_create_file("pm_genpd_summary", S_IRUGO, genpd_debugfs_dir,
2954 NULL, &summary_fops);
2956 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2957 d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
2959 debugfs_create_file("current_state", 0444,
2960 d, genpd, &status_fops);
2961 debugfs_create_file("sub_domains", 0444,
2962 d, genpd, &sub_domains_fops);
2963 debugfs_create_file("idle_states", 0444,
2964 d, genpd, &idle_states_fops);
2965 debugfs_create_file("active_time", 0444,
2966 d, genpd, &active_time_fops);
2967 debugfs_create_file("total_idle_time", 0444,
2968 d, genpd, &total_idle_time_fops);
2969 debugfs_create_file("devices", 0444,
2970 d, genpd, &devices_fops);
2971 if (genpd->set_performance_state)
2972 debugfs_create_file("perf_state", 0444,
2973 d, genpd, &perf_state_fops);
2978 late_initcall(genpd_debug_init);
2980 static void __exit genpd_debug_exit(void)
2982 debugfs_remove_recursive(genpd_debugfs_dir);
2984 __exitcall(genpd_debug_exit);
2985 #endif /* CONFIG_DEBUG_FS */