2 * drivers/base/power/domain.c - Common code related to device power domains.
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 * This file is released under the GPLv2.
9 #include <linux/delay.h>
10 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_opp.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/pm_domain.h>
16 #include <linux/pm_qos.h>
17 #include <linux/pm_clock.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/sched.h>
21 #include <linux/suspend.h>
22 #include <linux/export.h>
26 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
28 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
30 type (*__routine)(struct device *__d); \
31 type __ret = (type)0; \
33 __routine = genpd->dev_ops.callback; \
35 __ret = __routine(dev); \
40 static LIST_HEAD(gpd_list);
41 static DEFINE_MUTEX(gpd_list_lock);
43 struct genpd_lock_ops {
44 void (*lock)(struct generic_pm_domain *genpd);
45 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
46 int (*lock_interruptible)(struct generic_pm_domain *genpd);
47 void (*unlock)(struct generic_pm_domain *genpd);
50 static void genpd_lock_mtx(struct generic_pm_domain *genpd)
52 mutex_lock(&genpd->mlock);
55 static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
58 mutex_lock_nested(&genpd->mlock, depth);
61 static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
63 return mutex_lock_interruptible(&genpd->mlock);
66 static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
68 return mutex_unlock(&genpd->mlock);
71 static const struct genpd_lock_ops genpd_mtx_ops = {
72 .lock = genpd_lock_mtx,
73 .lock_nested = genpd_lock_nested_mtx,
74 .lock_interruptible = genpd_lock_interruptible_mtx,
75 .unlock = genpd_unlock_mtx,
78 static void genpd_lock_spin(struct generic_pm_domain *genpd)
79 __acquires(&genpd->slock)
83 spin_lock_irqsave(&genpd->slock, flags);
84 genpd->lock_flags = flags;
87 static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
89 __acquires(&genpd->slock)
93 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
94 genpd->lock_flags = flags;
97 static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
98 __acquires(&genpd->slock)
102 spin_lock_irqsave(&genpd->slock, flags);
103 genpd->lock_flags = flags;
107 static void genpd_unlock_spin(struct generic_pm_domain *genpd)
108 __releases(&genpd->slock)
110 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
113 static const struct genpd_lock_ops genpd_spin_ops = {
114 .lock = genpd_lock_spin,
115 .lock_nested = genpd_lock_nested_spin,
116 .lock_interruptible = genpd_lock_interruptible_spin,
117 .unlock = genpd_unlock_spin,
120 #define genpd_lock(p) p->lock_ops->lock(p)
121 #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
122 #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
123 #define genpd_unlock(p) p->lock_ops->unlock(p)
125 #define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
126 #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
127 #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
128 #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
130 static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
131 const struct generic_pm_domain *genpd)
135 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
138 * Warn once if an IRQ safe device is attached to a no sleep domain, as
139 * to indicate a suboptimal configuration for PM. For an always on
140 * domain this isn't case, thus don't warn.
142 if (ret && !genpd_is_always_on(genpd))
143 dev_warn_once(dev, "PM domain %s will not be powered off\n",
150 * Get the generic PM domain for a particular struct device.
151 * This validates the struct device pointer, the PM domain pointer,
152 * and checks that the PM domain pointer is a real generic PM domain.
153 * Any failure results in NULL being returned.
155 static struct generic_pm_domain *genpd_lookup_dev(struct device *dev)
157 struct generic_pm_domain *genpd = NULL, *gpd;
159 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
162 mutex_lock(&gpd_list_lock);
163 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
164 if (&gpd->domain == dev->pm_domain) {
169 mutex_unlock(&gpd_list_lock);
175 * This should only be used where we are certain that the pm_domain
176 * attached to the device is a genpd domain.
178 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
180 if (IS_ERR_OR_NULL(dev->pm_domain))
181 return ERR_PTR(-EINVAL);
183 return pd_to_genpd(dev->pm_domain);
186 static int genpd_stop_dev(const struct generic_pm_domain *genpd,
189 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
192 static int genpd_start_dev(const struct generic_pm_domain *genpd,
195 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
198 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
202 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
203 ret = !!atomic_dec_and_test(&genpd->sd_count);
208 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
210 atomic_inc(&genpd->sd_count);
211 smp_mb__after_atomic();
214 #ifdef CONFIG_DEBUG_FS
215 static void genpd_update_accounting(struct generic_pm_domain *genpd)
220 delta = ktime_sub(now, genpd->accounting_time);
223 * If genpd->status is active, it means we are just
224 * out of off and so update the idle time and vice
227 if (genpd->status == GPD_STATE_ACTIVE) {
228 int state_idx = genpd->state_idx;
230 genpd->states[state_idx].idle_time =
231 ktime_add(genpd->states[state_idx].idle_time, delta);
233 genpd->on_time = ktime_add(genpd->on_time, delta);
236 genpd->accounting_time = now;
239 static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
243 * dev_pm_genpd_set_performance_state- Set performance state of device's power
246 * @dev: Device for which the performance-state needs to be set.
247 * @state: Target performance state of the device. This can be set as 0 when the
248 * device doesn't have any performance state constraints left (And so
249 * the device wouldn't participate anymore to find the target
250 * performance state of the genpd).
252 * It is assumed that the users guarantee that the genpd wouldn't be detached
253 * while this routine is getting called.
255 * Returns 0 on success and negative error values on failures.
257 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
259 struct generic_pm_domain *genpd;
260 struct generic_pm_domain_data *gpd_data, *pd_data;
261 struct pm_domain_data *pdd;
265 genpd = dev_to_genpd(dev);
269 if (unlikely(!genpd->set_performance_state))
272 if (unlikely(!dev->power.subsys_data ||
273 !dev->power.subsys_data->domain_data)) {
280 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
281 prev = gpd_data->performance_state;
282 gpd_data->performance_state = state;
284 /* New requested state is same as Max requested state */
285 if (state == genpd->performance_state)
288 /* New requested state is higher than Max requested state */
289 if (state > genpd->performance_state)
292 /* Traverse all devices within the domain */
293 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
294 pd_data = to_gpd_data(pdd);
296 if (pd_data->performance_state > state)
297 state = pd_data->performance_state;
300 if (state == genpd->performance_state)
304 * We aren't propagating performance state changes of a subdomain to its
305 * masters as we don't have hardware that needs it. Over that, the
306 * performance states of subdomain and its masters may not have
307 * one-to-one mapping and would require additional information. We can
308 * get back to this once we have hardware that needs it. For that
309 * reason, we don't have to consider performance state of the subdomains
314 ret = genpd->set_performance_state(genpd, state);
316 gpd_data->performance_state = prev;
320 genpd->performance_state = state;
327 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
329 static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
331 unsigned int state_idx = genpd->state_idx;
336 if (!genpd->power_on)
340 return genpd->power_on(genpd);
342 time_start = ktime_get();
343 ret = genpd->power_on(genpd);
347 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
348 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
351 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
352 genpd->max_off_time_changed = true;
353 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
354 genpd->name, "on", elapsed_ns);
359 static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
361 unsigned int state_idx = genpd->state_idx;
366 if (!genpd->power_off)
370 return genpd->power_off(genpd);
372 time_start = ktime_get();
373 ret = genpd->power_off(genpd);
377 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
378 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
381 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
382 genpd->max_off_time_changed = true;
383 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
384 genpd->name, "off", elapsed_ns);
390 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
391 * @genpd: PM domain to power off.
393 * Queue up the execution of genpd_power_off() unless it's already been done
396 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
398 queue_work(pm_wq, &genpd->power_off_work);
402 * genpd_power_off - Remove power from a given PM domain.
403 * @genpd: PM domain to power down.
404 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
405 * RPM status of the releated device is in an intermediate state, not yet turned
406 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
407 * be RPM_SUSPENDED, while it tries to power off the PM domain.
409 * If all of the @genpd's devices have been suspended and all of its subdomains
410 * have been powered down, remove power from @genpd.
412 static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
415 struct pm_domain_data *pdd;
416 struct gpd_link *link;
417 unsigned int not_suspended = 0;
420 * Do not try to power off the domain in the following situations:
421 * (1) The domain is already in the "power off" state.
422 * (2) System suspend is in progress.
424 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
428 * Abort power off for the PM domain in the following situations:
429 * (1) The domain is configured as always on.
430 * (2) When the domain has a subdomain being powered on.
432 if (genpd_is_always_on(genpd) || atomic_read(&genpd->sd_count) > 0)
435 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
436 enum pm_qos_flags_status stat;
438 stat = dev_pm_qos_flags(pdd->dev, PM_QOS_FLAG_NO_POWER_OFF);
439 if (stat > PM_QOS_FLAGS_NONE)
443 * Do not allow PM domain to be powered off, when an IRQ safe
444 * device is part of a non-IRQ safe domain.
446 if (!pm_runtime_suspended(pdd->dev) ||
447 irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
451 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
454 if (genpd->gov && genpd->gov->power_down_ok) {
455 if (!genpd->gov->power_down_ok(&genpd->domain))
459 /* Default to shallowest state. */
461 genpd->state_idx = 0;
463 if (genpd->power_off) {
466 if (atomic_read(&genpd->sd_count) > 0)
470 * If sd_count > 0 at this point, one of the subdomains hasn't
471 * managed to call genpd_power_on() for the master yet after
472 * incrementing it. In that case genpd_power_on() will wait
473 * for us to drop the lock, so we can call .power_off() and let
474 * the genpd_power_on() restore power for us (this shouldn't
475 * happen very often).
477 ret = _genpd_power_off(genpd, true);
482 genpd->status = GPD_STATE_POWER_OFF;
483 genpd_update_accounting(genpd);
485 list_for_each_entry(link, &genpd->slave_links, slave_node) {
486 genpd_sd_counter_dec(link->master);
487 genpd_lock_nested(link->master, depth + 1);
488 genpd_power_off(link->master, false, depth + 1);
489 genpd_unlock(link->master);
496 * genpd_power_on - Restore power to a given PM domain and its masters.
497 * @genpd: PM domain to power up.
498 * @depth: nesting count for lockdep.
500 * Restore power to @genpd and all of its masters so that it is possible to
501 * resume a device belonging to it.
503 static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
505 struct gpd_link *link;
508 if (genpd_status_on(genpd))
512 * The list is guaranteed not to change while the loop below is being
513 * executed, unless one of the masters' .power_on() callbacks fiddles
516 list_for_each_entry(link, &genpd->slave_links, slave_node) {
517 struct generic_pm_domain *master = link->master;
519 genpd_sd_counter_inc(master);
521 genpd_lock_nested(master, depth + 1);
522 ret = genpd_power_on(master, depth + 1);
523 genpd_unlock(master);
526 genpd_sd_counter_dec(master);
531 ret = _genpd_power_on(genpd, true);
535 genpd->status = GPD_STATE_ACTIVE;
536 genpd_update_accounting(genpd);
541 list_for_each_entry_continue_reverse(link,
544 genpd_sd_counter_dec(link->master);
545 genpd_lock_nested(link->master, depth + 1);
546 genpd_power_off(link->master, false, depth + 1);
547 genpd_unlock(link->master);
553 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
554 unsigned long val, void *ptr)
556 struct generic_pm_domain_data *gpd_data;
559 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
560 dev = gpd_data->base.dev;
563 struct generic_pm_domain *genpd;
564 struct pm_domain_data *pdd;
566 spin_lock_irq(&dev->power.lock);
568 pdd = dev->power.subsys_data ?
569 dev->power.subsys_data->domain_data : NULL;
571 to_gpd_data(pdd)->td.constraint_changed = true;
572 genpd = dev_to_genpd(dev);
574 genpd = ERR_PTR(-ENODATA);
577 spin_unlock_irq(&dev->power.lock);
579 if (!IS_ERR(genpd)) {
581 genpd->max_off_time_changed = true;
586 if (!dev || dev->power.ignore_children)
594 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
595 * @work: Work structure used for scheduling the execution of this function.
597 static void genpd_power_off_work_fn(struct work_struct *work)
599 struct generic_pm_domain *genpd;
601 genpd = container_of(work, struct generic_pm_domain, power_off_work);
604 genpd_power_off(genpd, false, 0);
609 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
610 * @dev: Device to handle.
612 static int __genpd_runtime_suspend(struct device *dev)
614 int (*cb)(struct device *__dev);
616 if (dev->type && dev->type->pm)
617 cb = dev->type->pm->runtime_suspend;
618 else if (dev->class && dev->class->pm)
619 cb = dev->class->pm->runtime_suspend;
620 else if (dev->bus && dev->bus->pm)
621 cb = dev->bus->pm->runtime_suspend;
625 if (!cb && dev->driver && dev->driver->pm)
626 cb = dev->driver->pm->runtime_suspend;
628 return cb ? cb(dev) : 0;
632 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
633 * @dev: Device to handle.
635 static int __genpd_runtime_resume(struct device *dev)
637 int (*cb)(struct device *__dev);
639 if (dev->type && dev->type->pm)
640 cb = dev->type->pm->runtime_resume;
641 else if (dev->class && dev->class->pm)
642 cb = dev->class->pm->runtime_resume;
643 else if (dev->bus && dev->bus->pm)
644 cb = dev->bus->pm->runtime_resume;
648 if (!cb && dev->driver && dev->driver->pm)
649 cb = dev->driver->pm->runtime_resume;
651 return cb ? cb(dev) : 0;
655 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
656 * @dev: Device to suspend.
658 * Carry out a runtime suspend of a device under the assumption that its
659 * pm_domain field points to the domain member of an object of type
660 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
662 static int genpd_runtime_suspend(struct device *dev)
664 struct generic_pm_domain *genpd;
665 bool (*suspend_ok)(struct device *__dev);
666 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
667 bool runtime_pm = pm_runtime_enabled(dev);
672 dev_dbg(dev, "%s()\n", __func__);
674 genpd = dev_to_genpd(dev);
679 * A runtime PM centric subsystem/driver may re-use the runtime PM
680 * callbacks for other purposes than runtime PM. In those scenarios
681 * runtime PM is disabled. Under these circumstances, we shall skip
682 * validating/measuring the PM QoS latency.
684 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
685 if (runtime_pm && suspend_ok && !suspend_ok(dev))
688 /* Measure suspend latency. */
691 time_start = ktime_get();
693 ret = __genpd_runtime_suspend(dev);
697 ret = genpd_stop_dev(genpd, dev);
699 __genpd_runtime_resume(dev);
703 /* Update suspend latency value if the measured time exceeds it. */
705 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
706 if (elapsed_ns > td->suspend_latency_ns) {
707 td->suspend_latency_ns = elapsed_ns;
708 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
710 genpd->max_off_time_changed = true;
711 td->constraint_changed = true;
716 * If power.irq_safe is set, this routine may be run with
717 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
719 if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
723 genpd_power_off(genpd, true, 0);
730 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
731 * @dev: Device to resume.
733 * Carry out a runtime resume of a device under the assumption that its
734 * pm_domain field points to the domain member of an object of type
735 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
737 static int genpd_runtime_resume(struct device *dev)
739 struct generic_pm_domain *genpd;
740 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
741 bool runtime_pm = pm_runtime_enabled(dev);
747 dev_dbg(dev, "%s()\n", __func__);
749 genpd = dev_to_genpd(dev);
754 * As we don't power off a non IRQ safe domain, which holds
755 * an IRQ safe device, we don't need to restore power to it.
757 if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
763 ret = genpd_power_on(genpd, 0);
770 /* Measure resume latency. */
772 if (timed && runtime_pm)
773 time_start = ktime_get();
775 ret = genpd_start_dev(genpd, dev);
779 ret = __genpd_runtime_resume(dev);
783 /* Update resume latency value if the measured time exceeds it. */
784 if (timed && runtime_pm) {
785 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
786 if (elapsed_ns > td->resume_latency_ns) {
787 td->resume_latency_ns = elapsed_ns;
788 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
790 genpd->max_off_time_changed = true;
791 td->constraint_changed = true;
798 genpd_stop_dev(genpd, dev);
800 if (!pm_runtime_is_irq_safe(dev) ||
801 (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
803 genpd_power_off(genpd, true, 0);
810 static bool pd_ignore_unused;
811 static int __init pd_ignore_unused_setup(char *__unused)
813 pd_ignore_unused = true;
816 __setup("pd_ignore_unused", pd_ignore_unused_setup);
819 * genpd_power_off_unused - Power off all PM domains with no devices in use.
821 static int __init genpd_power_off_unused(void)
823 struct generic_pm_domain *genpd;
825 if (pd_ignore_unused) {
826 pr_warn("genpd: Not disabling unused power domains\n");
830 mutex_lock(&gpd_list_lock);
832 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
833 genpd_queue_power_off_work(genpd);
835 mutex_unlock(&gpd_list_lock);
839 late_initcall(genpd_power_off_unused);
841 #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
843 static bool genpd_present(const struct generic_pm_domain *genpd)
845 const struct generic_pm_domain *gpd;
847 if (IS_ERR_OR_NULL(genpd))
850 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
859 #ifdef CONFIG_PM_SLEEP
862 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
863 * @genpd: PM domain to power off, if possible.
864 * @use_lock: use the lock.
865 * @depth: nesting count for lockdep.
867 * Check if the given PM domain can be powered off (during system suspend or
868 * hibernation) and do that if so. Also, in that case propagate to its masters.
870 * This function is only called in "noirq" and "syscore" stages of system power
871 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
872 * these cases the lock must be held.
874 static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
877 struct gpd_link *link;
879 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
882 if (genpd->suspended_count != genpd->device_count
883 || atomic_read(&genpd->sd_count) > 0)
886 /* Choose the deepest state when suspending */
887 genpd->state_idx = genpd->state_count - 1;
888 if (_genpd_power_off(genpd, false))
891 genpd->status = GPD_STATE_POWER_OFF;
893 list_for_each_entry(link, &genpd->slave_links, slave_node) {
894 genpd_sd_counter_dec(link->master);
897 genpd_lock_nested(link->master, depth + 1);
899 genpd_sync_power_off(link->master, use_lock, depth + 1);
902 genpd_unlock(link->master);
907 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
908 * @genpd: PM domain to power on.
909 * @use_lock: use the lock.
910 * @depth: nesting count for lockdep.
912 * This function is only called in "noirq" and "syscore" stages of system power
913 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
914 * these cases the lock must be held.
916 static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
919 struct gpd_link *link;
921 if (genpd_status_on(genpd))
924 list_for_each_entry(link, &genpd->slave_links, slave_node) {
925 genpd_sd_counter_inc(link->master);
928 genpd_lock_nested(link->master, depth + 1);
930 genpd_sync_power_on(link->master, use_lock, depth + 1);
933 genpd_unlock(link->master);
936 _genpd_power_on(genpd, false);
938 genpd->status = GPD_STATE_ACTIVE;
942 * resume_needed - Check whether to resume a device before system suspend.
943 * @dev: Device to check.
944 * @genpd: PM domain the device belongs to.
946 * There are two cases in which a device that can wake up the system from sleep
947 * states should be resumed by genpd_prepare(): (1) if the device is enabled
948 * to wake up the system and it has to remain active for this purpose while the
949 * system is in the sleep state and (2) if the device is not enabled to wake up
950 * the system from sleep states and it generally doesn't generate wakeup signals
951 * by itself (those signals are generated on its behalf by other parts of the
952 * system). In the latter case it may be necessary to reconfigure the device's
953 * wakeup settings during system suspend, because it may have been set up to
954 * signal remote wakeup from the system's working state as needed by runtime PM.
955 * Return 'true' in either of the above cases.
957 static bool resume_needed(struct device *dev,
958 const struct generic_pm_domain *genpd)
962 if (!device_can_wakeup(dev))
965 active_wakeup = genpd_is_active_wakeup(genpd);
966 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
970 * genpd_prepare - Start power transition of a device in a PM domain.
971 * @dev: Device to start the transition of.
973 * Start a power transition of a device (during a system-wide power transition)
974 * under the assumption that its pm_domain field points to the domain member of
975 * an object of type struct generic_pm_domain representing a PM domain
976 * consisting of I/O devices.
978 static int genpd_prepare(struct device *dev)
980 struct generic_pm_domain *genpd;
983 dev_dbg(dev, "%s()\n", __func__);
985 genpd = dev_to_genpd(dev);
990 * If a wakeup request is pending for the device, it should be woken up
991 * at this point and a system wakeup event should be reported if it's
992 * set up to wake up the system from sleep states.
994 if (resume_needed(dev, genpd))
995 pm_runtime_resume(dev);
999 if (genpd->prepared_count++ == 0)
1000 genpd->suspended_count = 0;
1002 genpd_unlock(genpd);
1004 ret = pm_generic_prepare(dev);
1008 genpd->prepared_count--;
1010 genpd_unlock(genpd);
1013 /* Never return 1, as genpd don't cope with the direct_complete path. */
1014 return ret >= 0 ? 0 : ret;
1018 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1020 * @dev: Device to suspend.
1021 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
1023 * Stop the device and remove power from the domain if all devices in it have
1026 static int genpd_finish_suspend(struct device *dev, bool poweroff)
1028 struct generic_pm_domain *genpd;
1031 genpd = dev_to_genpd(dev);
1036 ret = pm_generic_poweroff_noirq(dev);
1038 ret = pm_generic_suspend_noirq(dev);
1042 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1045 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1046 !pm_runtime_status_suspended(dev)) {
1047 ret = genpd_stop_dev(genpd, dev);
1050 pm_generic_restore_noirq(dev);
1052 pm_generic_resume_noirq(dev);
1058 genpd->suspended_count++;
1059 genpd_sync_power_off(genpd, true, 0);
1060 genpd_unlock(genpd);
1066 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1067 * @dev: Device to suspend.
1069 * Stop the device and remove power from the domain if all devices in it have
1072 static int genpd_suspend_noirq(struct device *dev)
1074 dev_dbg(dev, "%s()\n", __func__);
1076 return genpd_finish_suspend(dev, false);
1080 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1081 * @dev: Device to resume.
1083 * Restore power to the device's PM domain, if necessary, and start the device.
1085 static int genpd_resume_noirq(struct device *dev)
1087 struct generic_pm_domain *genpd;
1090 dev_dbg(dev, "%s()\n", __func__);
1092 genpd = dev_to_genpd(dev);
1096 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1097 return pm_generic_resume_noirq(dev);
1100 genpd_sync_power_on(genpd, true, 0);
1101 genpd->suspended_count--;
1102 genpd_unlock(genpd);
1104 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1105 !pm_runtime_status_suspended(dev)) {
1106 ret = genpd_start_dev(genpd, dev);
1111 return pm_generic_resume_noirq(dev);
1115 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1116 * @dev: Device to freeze.
1118 * Carry out a late freeze of a device under the assumption that its
1119 * pm_domain field points to the domain member of an object of type
1120 * struct generic_pm_domain representing a power domain consisting of I/O
1123 static int genpd_freeze_noirq(struct device *dev)
1125 const struct generic_pm_domain *genpd;
1128 dev_dbg(dev, "%s()\n", __func__);
1130 genpd = dev_to_genpd(dev);
1134 ret = pm_generic_freeze_noirq(dev);
1138 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1139 !pm_runtime_status_suspended(dev))
1140 ret = genpd_stop_dev(genpd, dev);
1146 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1147 * @dev: Device to thaw.
1149 * Start the device, unless power has been removed from the domain already
1150 * before the system transition.
1152 static int genpd_thaw_noirq(struct device *dev)
1154 const struct generic_pm_domain *genpd;
1157 dev_dbg(dev, "%s()\n", __func__);
1159 genpd = dev_to_genpd(dev);
1163 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1164 !pm_runtime_status_suspended(dev)) {
1165 ret = genpd_start_dev(genpd, dev);
1170 return pm_generic_thaw_noirq(dev);
1174 * genpd_poweroff_noirq - Completion of hibernation of device in an
1176 * @dev: Device to poweroff.
1178 * Stop the device and remove power from the domain if all devices in it have
1181 static int genpd_poweroff_noirq(struct device *dev)
1183 dev_dbg(dev, "%s()\n", __func__);
1185 return genpd_finish_suspend(dev, true);
1189 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1190 * @dev: Device to resume.
1192 * Make sure the domain will be in the same power state as before the
1193 * hibernation the system is resuming from and start the device if necessary.
1195 static int genpd_restore_noirq(struct device *dev)
1197 struct generic_pm_domain *genpd;
1200 dev_dbg(dev, "%s()\n", __func__);
1202 genpd = dev_to_genpd(dev);
1207 * At this point suspended_count == 0 means we are being run for the
1208 * first time for the given domain in the present cycle.
1211 if (genpd->suspended_count++ == 0)
1213 * The boot kernel might put the domain into arbitrary state,
1214 * so make it appear as powered off to genpd_sync_power_on(),
1215 * so that it tries to power it on in case it was really off.
1217 genpd->status = GPD_STATE_POWER_OFF;
1219 genpd_sync_power_on(genpd, true, 0);
1220 genpd_unlock(genpd);
1222 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1223 !pm_runtime_status_suspended(dev)) {
1224 ret = genpd_start_dev(genpd, dev);
1229 return pm_generic_restore_noirq(dev);
1233 * genpd_complete - Complete power transition of a device in a power domain.
1234 * @dev: Device to complete the transition of.
1236 * Complete a power transition of a device (during a system-wide power
1237 * transition) under the assumption that its pm_domain field points to the
1238 * domain member of an object of type struct generic_pm_domain representing
1239 * a power domain consisting of I/O devices.
1241 static void genpd_complete(struct device *dev)
1243 struct generic_pm_domain *genpd;
1245 dev_dbg(dev, "%s()\n", __func__);
1247 genpd = dev_to_genpd(dev);
1251 pm_generic_complete(dev);
1255 genpd->prepared_count--;
1256 if (!genpd->prepared_count)
1257 genpd_queue_power_off_work(genpd);
1259 genpd_unlock(genpd);
1263 * genpd_syscore_switch - Switch power during system core suspend or resume.
1264 * @dev: Device that normally is marked as "always on" to switch power for.
1266 * This routine may only be called during the system core (syscore) suspend or
1267 * resume phase for devices whose "always on" flags are set.
1269 static void genpd_syscore_switch(struct device *dev, bool suspend)
1271 struct generic_pm_domain *genpd;
1273 genpd = dev_to_genpd(dev);
1274 if (!genpd_present(genpd))
1278 genpd->suspended_count++;
1279 genpd_sync_power_off(genpd, false, 0);
1281 genpd_sync_power_on(genpd, false, 0);
1282 genpd->suspended_count--;
1286 void pm_genpd_syscore_poweroff(struct device *dev)
1288 genpd_syscore_switch(dev, true);
1290 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1292 void pm_genpd_syscore_poweron(struct device *dev)
1294 genpd_syscore_switch(dev, false);
1296 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1298 #else /* !CONFIG_PM_SLEEP */
1300 #define genpd_prepare NULL
1301 #define genpd_suspend_noirq NULL
1302 #define genpd_resume_noirq NULL
1303 #define genpd_freeze_noirq NULL
1304 #define genpd_thaw_noirq NULL
1305 #define genpd_poweroff_noirq NULL
1306 #define genpd_restore_noirq NULL
1307 #define genpd_complete NULL
1309 #endif /* CONFIG_PM_SLEEP */
1311 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1312 struct gpd_timing_data *td)
1314 struct generic_pm_domain_data *gpd_data;
1317 ret = dev_pm_get_subsys_data(dev);
1319 return ERR_PTR(ret);
1321 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1330 gpd_data->base.dev = dev;
1331 gpd_data->td.constraint_changed = true;
1332 gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
1333 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1335 spin_lock_irq(&dev->power.lock);
1337 if (dev->power.subsys_data->domain_data) {
1342 dev->power.subsys_data->domain_data = &gpd_data->base;
1344 spin_unlock_irq(&dev->power.lock);
1349 spin_unlock_irq(&dev->power.lock);
1352 dev_pm_put_subsys_data(dev);
1353 return ERR_PTR(ret);
1356 static void genpd_free_dev_data(struct device *dev,
1357 struct generic_pm_domain_data *gpd_data)
1359 spin_lock_irq(&dev->power.lock);
1361 dev->power.subsys_data->domain_data = NULL;
1363 spin_unlock_irq(&dev->power.lock);
1366 dev_pm_put_subsys_data(dev);
1369 static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1370 struct gpd_timing_data *td)
1372 struct generic_pm_domain_data *gpd_data;
1375 dev_dbg(dev, "%s()\n", __func__);
1377 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1380 gpd_data = genpd_alloc_dev_data(dev, td);
1381 if (IS_ERR(gpd_data))
1382 return PTR_ERR(gpd_data);
1386 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1390 dev_pm_domain_set(dev, &genpd->domain);
1392 genpd->device_count++;
1393 genpd->max_off_time_changed = true;
1395 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1398 genpd_unlock(genpd);
1401 genpd_free_dev_data(dev, gpd_data);
1403 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1409 * pm_genpd_add_device - Add a device to an I/O PM domain.
1410 * @genpd: PM domain to add the device to.
1411 * @dev: Device to be added.
1413 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1417 mutex_lock(&gpd_list_lock);
1418 ret = genpd_add_device(genpd, dev, NULL);
1419 mutex_unlock(&gpd_list_lock);
1423 EXPORT_SYMBOL_GPL(pm_genpd_add_device);
1425 static int genpd_remove_device(struct generic_pm_domain *genpd,
1428 struct generic_pm_domain_data *gpd_data;
1429 struct pm_domain_data *pdd;
1432 dev_dbg(dev, "%s()\n", __func__);
1434 pdd = dev->power.subsys_data->domain_data;
1435 gpd_data = to_gpd_data(pdd);
1436 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1440 if (genpd->prepared_count > 0) {
1445 genpd->device_count--;
1446 genpd->max_off_time_changed = true;
1448 if (genpd->detach_dev)
1449 genpd->detach_dev(genpd, dev);
1451 dev_pm_domain_set(dev, NULL);
1453 list_del_init(&pdd->list_node);
1455 genpd_unlock(genpd);
1457 genpd_free_dev_data(dev, gpd_data);
1462 genpd_unlock(genpd);
1463 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1469 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1470 * @dev: Device to be removed.
1472 int pm_genpd_remove_device(struct device *dev)
1474 struct generic_pm_domain *genpd = genpd_lookup_dev(dev);
1479 return genpd_remove_device(genpd, dev);
1481 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1483 static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1484 struct generic_pm_domain *subdomain)
1486 struct gpd_link *link, *itr;
1489 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1490 || genpd == subdomain)
1494 * If the domain can be powered on/off in an IRQ safe
1495 * context, ensure that the subdomain can also be
1496 * powered on/off in that context.
1498 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1499 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1500 genpd->name, subdomain->name);
1504 link = kzalloc(sizeof(*link), GFP_KERNEL);
1508 genpd_lock(subdomain);
1509 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1511 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1516 list_for_each_entry(itr, &genpd->master_links, master_node) {
1517 if (itr->slave == subdomain && itr->master == genpd) {
1523 link->master = genpd;
1524 list_add_tail(&link->master_node, &genpd->master_links);
1525 link->slave = subdomain;
1526 list_add_tail(&link->slave_node, &subdomain->slave_links);
1527 if (genpd_status_on(subdomain))
1528 genpd_sd_counter_inc(genpd);
1531 genpd_unlock(genpd);
1532 genpd_unlock(subdomain);
1539 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1540 * @genpd: Master PM domain to add the subdomain to.
1541 * @subdomain: Subdomain to be added.
1543 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1544 struct generic_pm_domain *subdomain)
1548 mutex_lock(&gpd_list_lock);
1549 ret = genpd_add_subdomain(genpd, subdomain);
1550 mutex_unlock(&gpd_list_lock);
1554 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1557 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1558 * @genpd: Master PM domain to remove the subdomain from.
1559 * @subdomain: Subdomain to be removed.
1561 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1562 struct generic_pm_domain *subdomain)
1564 struct gpd_link *l, *link;
1567 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1570 genpd_lock(subdomain);
1571 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1573 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1574 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1580 list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
1581 if (link->slave != subdomain)
1584 list_del(&link->master_node);
1585 list_del(&link->slave_node);
1587 if (genpd_status_on(subdomain))
1588 genpd_sd_counter_dec(genpd);
1595 genpd_unlock(genpd);
1596 genpd_unlock(subdomain);
1600 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1602 static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1604 struct genpd_power_state *state;
1606 state = kzalloc(sizeof(*state), GFP_KERNEL);
1610 genpd->states = state;
1611 genpd->state_count = 1;
1612 genpd->free = state;
1617 static void genpd_lock_init(struct generic_pm_domain *genpd)
1619 if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1620 spin_lock_init(&genpd->slock);
1621 genpd->lock_ops = &genpd_spin_ops;
1623 mutex_init(&genpd->mlock);
1624 genpd->lock_ops = &genpd_mtx_ops;
1629 * pm_genpd_init - Initialize a generic I/O PM domain object.
1630 * @genpd: PM domain object to initialize.
1631 * @gov: PM domain governor to associate with the domain (may be NULL).
1632 * @is_off: Initial value of the domain's power_is_off field.
1634 * Returns 0 on successful initialization, else a negative error code.
1636 int pm_genpd_init(struct generic_pm_domain *genpd,
1637 struct dev_power_governor *gov, bool is_off)
1641 if (IS_ERR_OR_NULL(genpd))
1644 INIT_LIST_HEAD(&genpd->master_links);
1645 INIT_LIST_HEAD(&genpd->slave_links);
1646 INIT_LIST_HEAD(&genpd->dev_list);
1647 genpd_lock_init(genpd);
1649 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1650 atomic_set(&genpd->sd_count, 0);
1651 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1652 genpd->device_count = 0;
1653 genpd->max_off_time_ns = -1;
1654 genpd->max_off_time_changed = true;
1655 genpd->provider = NULL;
1656 genpd->has_provider = false;
1657 genpd->accounting_time = ktime_get();
1658 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1659 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
1660 genpd->domain.ops.prepare = genpd_prepare;
1661 genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
1662 genpd->domain.ops.resume_noirq = genpd_resume_noirq;
1663 genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
1664 genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
1665 genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
1666 genpd->domain.ops.restore_noirq = genpd_restore_noirq;
1667 genpd->domain.ops.complete = genpd_complete;
1669 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1670 genpd->dev_ops.stop = pm_clk_suspend;
1671 genpd->dev_ops.start = pm_clk_resume;
1674 /* Always-on domains must be powered on at initialization. */
1675 if (genpd_is_always_on(genpd) && !genpd_status_on(genpd))
1678 /* Use only one "off" state if there were no states declared */
1679 if (genpd->state_count == 0) {
1680 ret = genpd_set_default_power_state(genpd);
1684 pr_warn("%s : no governor for states\n", genpd->name);
1687 device_initialize(&genpd->dev);
1688 dev_set_name(&genpd->dev, "%s", genpd->name);
1690 mutex_lock(&gpd_list_lock);
1691 list_add(&genpd->gpd_list_node, &gpd_list);
1692 mutex_unlock(&gpd_list_lock);
1696 EXPORT_SYMBOL_GPL(pm_genpd_init);
1698 static int genpd_remove(struct generic_pm_domain *genpd)
1700 struct gpd_link *l, *link;
1702 if (IS_ERR_OR_NULL(genpd))
1707 if (genpd->has_provider) {
1708 genpd_unlock(genpd);
1709 pr_err("Provider present, unable to remove %s\n", genpd->name);
1713 if (!list_empty(&genpd->master_links) || genpd->device_count) {
1714 genpd_unlock(genpd);
1715 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1719 list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
1720 list_del(&link->master_node);
1721 list_del(&link->slave_node);
1725 list_del(&genpd->gpd_list_node);
1726 genpd_unlock(genpd);
1727 cancel_work_sync(&genpd->power_off_work);
1729 pr_debug("%s: removed %s\n", __func__, genpd->name);
1735 * pm_genpd_remove - Remove a generic I/O PM domain
1736 * @genpd: Pointer to PM domain that is to be removed.
1738 * To remove the PM domain, this function:
1739 * - Removes the PM domain as a subdomain to any parent domains,
1741 * - Removes the PM domain from the list of registered PM domains.
1743 * The PM domain will only be removed, if the associated provider has
1744 * been removed, it is not a parent to any other PM domain and has no
1745 * devices associated with it.
1747 int pm_genpd_remove(struct generic_pm_domain *genpd)
1751 mutex_lock(&gpd_list_lock);
1752 ret = genpd_remove(genpd);
1753 mutex_unlock(&gpd_list_lock);
1757 EXPORT_SYMBOL_GPL(pm_genpd_remove);
1759 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1762 * Device Tree based PM domain providers.
1764 * The code below implements generic device tree based PM domain providers that
1765 * bind device tree nodes with generic PM domains registered in the system.
1767 * Any driver that registers generic PM domains and needs to support binding of
1768 * devices to these domains is supposed to register a PM domain provider, which
1769 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1771 * Two simple mapping functions have been provided for convenience:
1772 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1773 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1778 * struct of_genpd_provider - PM domain provider registration structure
1779 * @link: Entry in global list of PM domain providers
1780 * @node: Pointer to device tree node of PM domain provider
1781 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1783 * @data: context pointer to be passed into @xlate callback
1785 struct of_genpd_provider {
1786 struct list_head link;
1787 struct device_node *node;
1788 genpd_xlate_t xlate;
1792 /* List of registered PM domain providers. */
1793 static LIST_HEAD(of_genpd_providers);
1794 /* Mutex to protect the list above. */
1795 static DEFINE_MUTEX(of_genpd_mutex);
1798 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1799 * @genpdspec: OF phandle args to map into a PM domain
1800 * @data: xlate function private data - pointer to struct generic_pm_domain
1802 * This is a generic xlate function that can be used to model PM domains that
1803 * have their own device tree nodes. The private data of xlate function needs
1804 * to be a valid pointer to struct generic_pm_domain.
1806 static struct generic_pm_domain *genpd_xlate_simple(
1807 struct of_phandle_args *genpdspec,
1814 * genpd_xlate_onecell() - Xlate function using a single index.
1815 * @genpdspec: OF phandle args to map into a PM domain
1816 * @data: xlate function private data - pointer to struct genpd_onecell_data
1818 * This is a generic xlate function that can be used to model simple PM domain
1819 * controllers that have one device tree node and provide multiple PM domains.
1820 * A single cell is used as an index into an array of PM domains specified in
1821 * the genpd_onecell_data struct when registering the provider.
1823 static struct generic_pm_domain *genpd_xlate_onecell(
1824 struct of_phandle_args *genpdspec,
1827 struct genpd_onecell_data *genpd_data = data;
1828 unsigned int idx = genpdspec->args[0];
1830 if (genpdspec->args_count != 1)
1831 return ERR_PTR(-EINVAL);
1833 if (idx >= genpd_data->num_domains) {
1834 pr_err("%s: invalid domain index %u\n", __func__, idx);
1835 return ERR_PTR(-EINVAL);
1838 if (!genpd_data->domains[idx])
1839 return ERR_PTR(-ENOENT);
1841 return genpd_data->domains[idx];
1845 * genpd_add_provider() - Register a PM domain provider for a node
1846 * @np: Device node pointer associated with the PM domain provider.
1847 * @xlate: Callback for decoding PM domain from phandle arguments.
1848 * @data: Context pointer for @xlate callback.
1850 static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1853 struct of_genpd_provider *cp;
1855 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1859 cp->node = of_node_get(np);
1863 mutex_lock(&of_genpd_mutex);
1864 list_add(&cp->link, &of_genpd_providers);
1865 mutex_unlock(&of_genpd_mutex);
1866 pr_debug("Added domain provider from %pOF\n", np);
1872 * of_genpd_add_provider_simple() - Register a simple PM domain provider
1873 * @np: Device node pointer associated with the PM domain provider.
1874 * @genpd: Pointer to PM domain associated with the PM domain provider.
1876 int of_genpd_add_provider_simple(struct device_node *np,
1877 struct generic_pm_domain *genpd)
1884 mutex_lock(&gpd_list_lock);
1886 if (!genpd_present(genpd))
1889 genpd->dev.of_node = np;
1891 /* Parse genpd OPP table */
1892 if (genpd->set_performance_state) {
1893 ret = dev_pm_opp_of_add_table(&genpd->dev);
1895 dev_err(&genpd->dev, "Failed to add OPP table: %d\n",
1901 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
1903 if (genpd->set_performance_state)
1904 dev_pm_opp_of_remove_table(&genpd->dev);
1909 genpd->provider = &np->fwnode;
1910 genpd->has_provider = true;
1913 mutex_unlock(&gpd_list_lock);
1917 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
1920 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
1921 * @np: Device node pointer associated with the PM domain provider.
1922 * @data: Pointer to the data associated with the PM domain provider.
1924 int of_genpd_add_provider_onecell(struct device_node *np,
1925 struct genpd_onecell_data *data)
1927 struct generic_pm_domain *genpd;
1934 mutex_lock(&gpd_list_lock);
1937 data->xlate = genpd_xlate_onecell;
1939 for (i = 0; i < data->num_domains; i++) {
1940 genpd = data->domains[i];
1944 if (!genpd_present(genpd))
1947 genpd->dev.of_node = np;
1949 /* Parse genpd OPP table */
1950 if (genpd->set_performance_state) {
1951 ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
1953 dev_err(&genpd->dev, "Failed to add OPP table for index %d: %d\n",
1959 genpd->provider = &np->fwnode;
1960 genpd->has_provider = true;
1963 ret = genpd_add_provider(np, data->xlate, data);
1967 mutex_unlock(&gpd_list_lock);
1973 genpd = data->domains[i];
1978 genpd->provider = NULL;
1979 genpd->has_provider = false;
1981 if (genpd->set_performance_state)
1982 dev_pm_opp_of_remove_table(&genpd->dev);
1985 mutex_unlock(&gpd_list_lock);
1989 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
1992 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1993 * @np: Device node pointer associated with the PM domain provider
1995 void of_genpd_del_provider(struct device_node *np)
1997 struct of_genpd_provider *cp, *tmp;
1998 struct generic_pm_domain *gpd;
2000 mutex_lock(&gpd_list_lock);
2001 mutex_lock(&of_genpd_mutex);
2002 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
2003 if (cp->node == np) {
2005 * For each PM domain associated with the
2006 * provider, set the 'has_provider' to false
2007 * so that the PM domain can be safely removed.
2009 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2010 if (gpd->provider == &np->fwnode) {
2011 gpd->has_provider = false;
2013 if (!gpd->set_performance_state)
2016 dev_pm_opp_of_remove_table(&gpd->dev);
2020 list_del(&cp->link);
2021 of_node_put(cp->node);
2026 mutex_unlock(&of_genpd_mutex);
2027 mutex_unlock(&gpd_list_lock);
2029 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2032 * genpd_get_from_provider() - Look-up PM domain
2033 * @genpdspec: OF phandle args to use for look-up
2035 * Looks for a PM domain provider under the node specified by @genpdspec and if
2036 * found, uses xlate function of the provider to map phandle args to a PM
2039 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2042 static struct generic_pm_domain *genpd_get_from_provider(
2043 struct of_phandle_args *genpdspec)
2045 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2046 struct of_genpd_provider *provider;
2049 return ERR_PTR(-EINVAL);
2051 mutex_lock(&of_genpd_mutex);
2053 /* Check if we have such a provider in our array */
2054 list_for_each_entry(provider, &of_genpd_providers, link) {
2055 if (provider->node == genpdspec->np)
2056 genpd = provider->xlate(genpdspec, provider->data);
2061 mutex_unlock(&of_genpd_mutex);
2067 * of_genpd_add_device() - Add a device to an I/O PM domain
2068 * @genpdspec: OF phandle args to use for look-up PM domain
2069 * @dev: Device to be added.
2071 * Looks-up an I/O PM domain based upon phandle args provided and adds
2072 * the device to the PM domain. Returns a negative error code on failure.
2074 int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
2076 struct generic_pm_domain *genpd;
2079 mutex_lock(&gpd_list_lock);
2081 genpd = genpd_get_from_provider(genpdspec);
2082 if (IS_ERR(genpd)) {
2083 ret = PTR_ERR(genpd);
2087 ret = genpd_add_device(genpd, dev, NULL);
2090 mutex_unlock(&gpd_list_lock);
2094 EXPORT_SYMBOL_GPL(of_genpd_add_device);
2097 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2098 * @parent_spec: OF phandle args to use for parent PM domain look-up
2099 * @subdomain_spec: OF phandle args to use for subdomain look-up
2101 * Looks-up a parent PM domain and subdomain based upon phandle args
2102 * provided and adds the subdomain to the parent PM domain. Returns a
2103 * negative error code on failure.
2105 int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
2106 struct of_phandle_args *subdomain_spec)
2108 struct generic_pm_domain *parent, *subdomain;
2111 mutex_lock(&gpd_list_lock);
2113 parent = genpd_get_from_provider(parent_spec);
2114 if (IS_ERR(parent)) {
2115 ret = PTR_ERR(parent);
2119 subdomain = genpd_get_from_provider(subdomain_spec);
2120 if (IS_ERR(subdomain)) {
2121 ret = PTR_ERR(subdomain);
2125 ret = genpd_add_subdomain(parent, subdomain);
2128 mutex_unlock(&gpd_list_lock);
2132 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2135 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2136 * @provider: Pointer to device structure associated with provider
2138 * Find the last PM domain that was added by a particular provider and
2139 * remove this PM domain from the list of PM domains. The provider is
2140 * identified by the 'provider' device structure that is passed. The PM
2141 * domain will only be removed, if the provider associated with domain
2144 * Returns a valid pointer to struct generic_pm_domain on success or
2145 * ERR_PTR() on failure.
2147 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2149 struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2152 if (IS_ERR_OR_NULL(np))
2153 return ERR_PTR(-EINVAL);
2155 mutex_lock(&gpd_list_lock);
2156 list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2157 if (gpd->provider == &np->fwnode) {
2158 ret = genpd_remove(gpd);
2159 genpd = ret ? ERR_PTR(ret) : gpd;
2163 mutex_unlock(&gpd_list_lock);
2167 EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2169 static void genpd_release_dev(struct device *dev)
2174 static struct bus_type genpd_bus_type = {
2179 * genpd_dev_pm_detach - Detach a device from its PM domain.
2180 * @dev: Device to detach.
2181 * @power_off: Currently not used
2183 * Try to locate a corresponding generic PM domain, which the device was
2184 * attached to previously. If such is found, the device is detached from it.
2186 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2188 struct generic_pm_domain *pd;
2192 pd = dev_to_genpd(dev);
2196 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2198 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2199 ret = genpd_remove_device(pd, dev);
2208 dev_err(dev, "failed to remove from PM domain %s: %d",
2213 /* Check if PM domain can be powered off after removing this device. */
2214 genpd_queue_power_off_work(pd);
2216 /* Unregister the device if it was created by genpd. */
2217 if (dev->bus == &genpd_bus_type)
2218 device_unregister(dev);
2221 static void genpd_dev_pm_sync(struct device *dev)
2223 struct generic_pm_domain *pd;
2225 pd = dev_to_genpd(dev);
2229 genpd_queue_power_off_work(pd);
2232 static int __genpd_dev_pm_attach(struct device *dev, struct device_node *np,
2233 unsigned int index, bool power_on)
2235 struct of_phandle_args pd_args;
2236 struct generic_pm_domain *pd;
2239 ret = of_parse_phandle_with_args(np, "power-domains",
2240 "#power-domain-cells", index, &pd_args);
2244 mutex_lock(&gpd_list_lock);
2245 pd = genpd_get_from_provider(&pd_args);
2246 of_node_put(pd_args.np);
2248 mutex_unlock(&gpd_list_lock);
2249 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2250 __func__, PTR_ERR(pd));
2251 return driver_deferred_probe_check_state(dev);
2254 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2256 ret = genpd_add_device(pd, dev, NULL);
2257 mutex_unlock(&gpd_list_lock);
2260 if (ret != -EPROBE_DEFER)
2261 dev_err(dev, "failed to add to PM domain %s: %d",
2266 dev->pm_domain->detach = genpd_dev_pm_detach;
2267 dev->pm_domain->sync = genpd_dev_pm_sync;
2271 ret = genpd_power_on(pd, 0);
2276 genpd_remove_device(pd, dev);
2278 return ret ? -EPROBE_DEFER : 1;
2282 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2283 * @dev: Device to attach.
2285 * Parse device's OF node to find a PM domain specifier. If such is found,
2286 * attaches the device to retrieved pm_domain ops.
2288 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2289 * PM domain or when multiple power-domains exists for it, else a negative error
2290 * code. Note that if a power-domain exists for the device, but it cannot be
2291 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2292 * not probed and to re-try again later.
2294 int genpd_dev_pm_attach(struct device *dev)
2300 * Devices with multiple PM domains must be attached separately, as we
2301 * can only attach one PM domain per device.
2303 if (of_count_phandle_with_args(dev->of_node, "power-domains",
2304 "#power-domain-cells") != 1)
2307 return __genpd_dev_pm_attach(dev, dev->of_node, 0, true);
2309 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2312 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2313 * @dev: The device used to lookup the PM domain.
2314 * @index: The index of the PM domain.
2316 * Parse device's OF node to find a PM domain specifier at the provided @index.
2317 * If such is found, creates a virtual device and attaches it to the retrieved
2318 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2319 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2321 * Returns the created virtual device if successfully attached PM domain, NULL
2322 * when the device don't need a PM domain, else an ERR_PTR() in case of
2323 * failures. If a power-domain exists for the device, but cannot be found or
2324 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2325 * is not probed and to re-try again later.
2327 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
2330 struct device *virt_dev;
2337 /* Deal only with devices using multiple PM domains. */
2338 num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
2339 "#power-domain-cells");
2340 if (num_domains < 2 || index >= num_domains)
2343 /* Allocate and register device on the genpd bus. */
2344 virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL);
2346 return ERR_PTR(-ENOMEM);
2348 dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev));
2349 virt_dev->bus = &genpd_bus_type;
2350 virt_dev->release = genpd_release_dev;
2352 ret = device_register(virt_dev);
2355 return ERR_PTR(ret);
2358 /* Try to attach the device to the PM domain at the specified index. */
2359 ret = __genpd_dev_pm_attach(virt_dev, dev->of_node, index, false);
2361 device_unregister(virt_dev);
2362 return ret ? ERR_PTR(ret) : NULL;
2365 pm_runtime_enable(virt_dev);
2366 genpd_queue_power_off_work(dev_to_genpd(virt_dev));
2370 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
2373 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
2374 * @dev: The device used to lookup the PM domain.
2375 * @name: The name of the PM domain.
2377 * Parse device's OF node to find a PM domain specifier using the
2378 * power-domain-names DT property. For further description see
2379 * genpd_dev_pm_attach_by_id().
2381 struct device *genpd_dev_pm_attach_by_name(struct device *dev, char *name)
2388 index = of_property_match_string(dev->of_node, "power-domain-names",
2393 return genpd_dev_pm_attach_by_id(dev, index);
2396 static const struct of_device_id idle_state_match[] = {
2397 { .compatible = "domain-idle-state", },
2401 static int genpd_parse_state(struct genpd_power_state *genpd_state,
2402 struct device_node *state_node)
2406 u32 entry_latency, exit_latency;
2408 err = of_property_read_u32(state_node, "entry-latency-us",
2411 pr_debug(" * %pOF missing entry-latency-us property\n",
2416 err = of_property_read_u32(state_node, "exit-latency-us",
2419 pr_debug(" * %pOF missing exit-latency-us property\n",
2424 err = of_property_read_u32(state_node, "min-residency-us", &residency);
2426 genpd_state->residency_ns = 1000 * residency;
2428 genpd_state->power_on_latency_ns = 1000 * exit_latency;
2429 genpd_state->power_off_latency_ns = 1000 * entry_latency;
2430 genpd_state->fwnode = &state_node->fwnode;
2435 static int genpd_iterate_idle_states(struct device_node *dn,
2436 struct genpd_power_state *states)
2439 struct of_phandle_iterator it;
2440 struct device_node *np;
2443 ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2447 /* Loop over the phandles until all the requested entry is found */
2448 of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
2450 if (!of_match_node(idle_state_match, np))
2453 ret = genpd_parse_state(&states[i], np);
2455 pr_err("Parsing idle state node %pOF failed with err %d\n",
2468 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2470 * @dn: The genpd device node
2471 * @states: The pointer to which the state array will be saved.
2472 * @n: The count of elements in the array returned from this function.
2474 * Returns the device states parsed from the OF node. The memory for the states
2475 * is allocated by this function and is the responsibility of the caller to
2476 * free the memory after use. If any or zero compatible domain idle states is
2477 * found it returns 0 and in case of errors, a negative error code is returned.
2479 int of_genpd_parse_idle_states(struct device_node *dn,
2480 struct genpd_power_state **states, int *n)
2482 struct genpd_power_state *st;
2485 ret = genpd_iterate_idle_states(dn, NULL);
2495 st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
2499 ret = genpd_iterate_idle_states(dn, st);
2502 return ret < 0 ? ret : -EINVAL;
2510 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2513 * pm_genpd_opp_to_performance_state - Gets performance state of the genpd from its OPP node.
2515 * @genpd_dev: Genpd's device for which the performance-state needs to be found.
2516 * @opp: struct dev_pm_opp of the OPP for which we need to find performance
2519 * Returns performance state encoded in the OPP of the genpd. This calls
2520 * platform specific genpd->opp_to_performance_state() callback to translate
2521 * power domain OPP to performance state.
2523 * Returns performance state on success and 0 on failure.
2525 unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
2526 struct dev_pm_opp *opp)
2528 struct generic_pm_domain *genpd = NULL;
2531 genpd = container_of(genpd_dev, struct generic_pm_domain, dev);
2533 if (unlikely(!genpd->opp_to_performance_state))
2537 state = genpd->opp_to_performance_state(genpd, opp);
2538 genpd_unlock(genpd);
2542 EXPORT_SYMBOL_GPL(pm_genpd_opp_to_performance_state);
2544 static int __init genpd_bus_init(void)
2546 return bus_register(&genpd_bus_type);
2548 core_initcall(genpd_bus_init);
2550 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2553 /*** debugfs support ***/
2555 #ifdef CONFIG_DEBUG_FS
2556 #include <linux/pm.h>
2557 #include <linux/device.h>
2558 #include <linux/debugfs.h>
2559 #include <linux/seq_file.h>
2560 #include <linux/init.h>
2561 #include <linux/kobject.h>
2562 static struct dentry *genpd_debugfs_dir;
2565 * TODO: This function is a slightly modified version of rtpm_status_show
2566 * from sysfs.c, so generalize it.
2568 static void rtpm_status_str(struct seq_file *s, struct device *dev)
2570 static const char * const status_lookup[] = {
2571 [RPM_ACTIVE] = "active",
2572 [RPM_RESUMING] = "resuming",
2573 [RPM_SUSPENDED] = "suspended",
2574 [RPM_SUSPENDING] = "suspending"
2578 if (dev->power.runtime_error)
2580 else if (dev->power.disable_depth)
2582 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2583 p = status_lookup[dev->power.runtime_status];
2590 static int genpd_summary_one(struct seq_file *s,
2591 struct generic_pm_domain *genpd)
2593 static const char * const status_lookup[] = {
2594 [GPD_STATE_ACTIVE] = "on",
2595 [GPD_STATE_POWER_OFF] = "off"
2597 struct pm_domain_data *pm_data;
2598 const char *kobj_path;
2599 struct gpd_link *link;
2603 ret = genpd_lock_interruptible(genpd);
2605 return -ERESTARTSYS;
2607 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2609 if (!genpd_status_on(genpd))
2610 snprintf(state, sizeof(state), "%s-%u",
2611 status_lookup[genpd->status], genpd->state_idx);
2613 snprintf(state, sizeof(state), "%s",
2614 status_lookup[genpd->status]);
2615 seq_printf(s, "%-30s %-15s ", genpd->name, state);
2618 * Modifications on the list require holding locks on both
2619 * master and slave, so we are safe.
2620 * Also genpd->name is immutable.
2622 list_for_each_entry(link, &genpd->master_links, master_node) {
2623 seq_printf(s, "%s", link->slave->name);
2624 if (!list_is_last(&link->master_node, &genpd->master_links))
2628 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2629 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2630 genpd_is_irq_safe(genpd) ?
2631 GFP_ATOMIC : GFP_KERNEL);
2632 if (kobj_path == NULL)
2635 seq_printf(s, "\n %-50s ", kobj_path);
2636 rtpm_status_str(s, pm_data->dev);
2642 genpd_unlock(genpd);
2647 static int genpd_summary_show(struct seq_file *s, void *data)
2649 struct generic_pm_domain *genpd;
2652 seq_puts(s, "domain status slaves\n");
2653 seq_puts(s, " /device runtime status\n");
2654 seq_puts(s, "----------------------------------------------------------------------\n");
2656 ret = mutex_lock_interruptible(&gpd_list_lock);
2658 return -ERESTARTSYS;
2660 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2661 ret = genpd_summary_one(s, genpd);
2665 mutex_unlock(&gpd_list_lock);
2670 static int genpd_status_show(struct seq_file *s, void *data)
2672 static const char * const status_lookup[] = {
2673 [GPD_STATE_ACTIVE] = "on",
2674 [GPD_STATE_POWER_OFF] = "off"
2677 struct generic_pm_domain *genpd = s->private;
2680 ret = genpd_lock_interruptible(genpd);
2682 return -ERESTARTSYS;
2684 if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
2687 if (genpd->status == GPD_STATE_POWER_OFF)
2688 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
2691 seq_printf(s, "%s\n", status_lookup[genpd->status]);
2693 genpd_unlock(genpd);
2697 static int genpd_sub_domains_show(struct seq_file *s, void *data)
2699 struct generic_pm_domain *genpd = s->private;
2700 struct gpd_link *link;
2703 ret = genpd_lock_interruptible(genpd);
2705 return -ERESTARTSYS;
2707 list_for_each_entry(link, &genpd->master_links, master_node)
2708 seq_printf(s, "%s\n", link->slave->name);
2710 genpd_unlock(genpd);
2714 static int genpd_idle_states_show(struct seq_file *s, void *data)
2716 struct generic_pm_domain *genpd = s->private;
2720 ret = genpd_lock_interruptible(genpd);
2722 return -ERESTARTSYS;
2724 seq_puts(s, "State Time Spent(ms)\n");
2726 for (i = 0; i < genpd->state_count; i++) {
2730 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2731 (genpd->state_idx == i))
2732 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2734 msecs = ktime_to_ms(
2735 ktime_add(genpd->states[i].idle_time, delta));
2736 seq_printf(s, "S%-13i %lld\n", i, msecs);
2739 genpd_unlock(genpd);
2743 static int genpd_active_time_show(struct seq_file *s, void *data)
2745 struct generic_pm_domain *genpd = s->private;
2749 ret = genpd_lock_interruptible(genpd);
2751 return -ERESTARTSYS;
2753 if (genpd->status == GPD_STATE_ACTIVE)
2754 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2756 seq_printf(s, "%lld ms\n", ktime_to_ms(
2757 ktime_add(genpd->on_time, delta)));
2759 genpd_unlock(genpd);
2763 static int genpd_total_idle_time_show(struct seq_file *s, void *data)
2765 struct generic_pm_domain *genpd = s->private;
2766 ktime_t delta = 0, total = 0;
2770 ret = genpd_lock_interruptible(genpd);
2772 return -ERESTARTSYS;
2774 for (i = 0; i < genpd->state_count; i++) {
2776 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2777 (genpd->state_idx == i))
2778 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2780 total = ktime_add(total, genpd->states[i].idle_time);
2782 total = ktime_add(total, delta);
2784 seq_printf(s, "%lld ms\n", ktime_to_ms(total));
2786 genpd_unlock(genpd);
2791 static int genpd_devices_show(struct seq_file *s, void *data)
2793 struct generic_pm_domain *genpd = s->private;
2794 struct pm_domain_data *pm_data;
2795 const char *kobj_path;
2798 ret = genpd_lock_interruptible(genpd);
2800 return -ERESTARTSYS;
2802 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2803 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2804 genpd_is_irq_safe(genpd) ?
2805 GFP_ATOMIC : GFP_KERNEL);
2806 if (kobj_path == NULL)
2809 seq_printf(s, "%s\n", kobj_path);
2813 genpd_unlock(genpd);
2817 static int genpd_perf_state_show(struct seq_file *s, void *data)
2819 struct generic_pm_domain *genpd = s->private;
2821 if (genpd_lock_interruptible(genpd))
2822 return -ERESTARTSYS;
2824 seq_printf(s, "%u\n", genpd->performance_state);
2826 genpd_unlock(genpd);
2830 #define define_genpd_open_function(name) \
2831 static int genpd_##name##_open(struct inode *inode, struct file *file) \
2833 return single_open(file, genpd_##name##_show, inode->i_private); \
2836 define_genpd_open_function(summary);
2837 define_genpd_open_function(status);
2838 define_genpd_open_function(sub_domains);
2839 define_genpd_open_function(idle_states);
2840 define_genpd_open_function(active_time);
2841 define_genpd_open_function(total_idle_time);
2842 define_genpd_open_function(devices);
2843 define_genpd_open_function(perf_state);
2845 #define define_genpd_debugfs_fops(name) \
2846 static const struct file_operations genpd_##name##_fops = { \
2847 .open = genpd_##name##_open, \
2849 .llseek = seq_lseek, \
2850 .release = single_release, \
2853 define_genpd_debugfs_fops(summary);
2854 define_genpd_debugfs_fops(status);
2855 define_genpd_debugfs_fops(sub_domains);
2856 define_genpd_debugfs_fops(idle_states);
2857 define_genpd_debugfs_fops(active_time);
2858 define_genpd_debugfs_fops(total_idle_time);
2859 define_genpd_debugfs_fops(devices);
2860 define_genpd_debugfs_fops(perf_state);
2862 static int __init genpd_debug_init(void)
2865 struct generic_pm_domain *genpd;
2867 genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2869 if (!genpd_debugfs_dir)
2872 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
2873 genpd_debugfs_dir, NULL, &genpd_summary_fops);
2877 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2878 d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
2882 debugfs_create_file("current_state", 0444,
2883 d, genpd, &genpd_status_fops);
2884 debugfs_create_file("sub_domains", 0444,
2885 d, genpd, &genpd_sub_domains_fops);
2886 debugfs_create_file("idle_states", 0444,
2887 d, genpd, &genpd_idle_states_fops);
2888 debugfs_create_file("active_time", 0444,
2889 d, genpd, &genpd_active_time_fops);
2890 debugfs_create_file("total_idle_time", 0444,
2891 d, genpd, &genpd_total_idle_time_fops);
2892 debugfs_create_file("devices", 0444,
2893 d, genpd, &genpd_devices_fops);
2894 if (genpd->set_performance_state)
2895 debugfs_create_file("perf_state", 0444,
2896 d, genpd, &genpd_perf_state_fops);
2901 late_initcall(genpd_debug_init);
2903 static void __exit genpd_debug_exit(void)
2905 debugfs_remove_recursive(genpd_debugfs_dir);
2907 __exitcall(genpd_debug_exit);
2908 #endif /* CONFIG_DEBUG_FS */