2 * drivers/base/power/domain.c - Common code related to device power domains.
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 * This file is released under the GPLv2.
9 #include <linux/delay.h>
10 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/pm_domain.h>
15 #include <linux/pm_qos.h>
16 #include <linux/pm_clock.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/sched.h>
20 #include <linux/suspend.h>
21 #include <linux/export.h>
25 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
27 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
29 type (*__routine)(struct device *__d); \
30 type __ret = (type)0; \
32 __routine = genpd->dev_ops.callback; \
34 __ret = __routine(dev); \
39 static LIST_HEAD(gpd_list);
40 static DEFINE_MUTEX(gpd_list_lock);
42 struct genpd_lock_ops {
43 void (*lock)(struct generic_pm_domain *genpd);
44 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
45 int (*lock_interruptible)(struct generic_pm_domain *genpd);
46 void (*unlock)(struct generic_pm_domain *genpd);
49 static void genpd_lock_mtx(struct generic_pm_domain *genpd)
51 mutex_lock(&genpd->mlock);
54 static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
57 mutex_lock_nested(&genpd->mlock, depth);
60 static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
62 return mutex_lock_interruptible(&genpd->mlock);
65 static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
67 return mutex_unlock(&genpd->mlock);
70 static const struct genpd_lock_ops genpd_mtx_ops = {
71 .lock = genpd_lock_mtx,
72 .lock_nested = genpd_lock_nested_mtx,
73 .lock_interruptible = genpd_lock_interruptible_mtx,
74 .unlock = genpd_unlock_mtx,
77 static void genpd_lock_spin(struct generic_pm_domain *genpd)
78 __acquires(&genpd->slock)
82 spin_lock_irqsave(&genpd->slock, flags);
83 genpd->lock_flags = flags;
86 static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
88 __acquires(&genpd->slock)
92 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
93 genpd->lock_flags = flags;
96 static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
97 __acquires(&genpd->slock)
101 spin_lock_irqsave(&genpd->slock, flags);
102 genpd->lock_flags = flags;
106 static void genpd_unlock_spin(struct generic_pm_domain *genpd)
107 __releases(&genpd->slock)
109 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
112 static const struct genpd_lock_ops genpd_spin_ops = {
113 .lock = genpd_lock_spin,
114 .lock_nested = genpd_lock_nested_spin,
115 .lock_interruptible = genpd_lock_interruptible_spin,
116 .unlock = genpd_unlock_spin,
119 #define genpd_lock(p) p->lock_ops->lock(p)
120 #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
121 #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
122 #define genpd_unlock(p) p->lock_ops->unlock(p)
124 #define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
125 #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
126 #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
128 static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
129 const struct generic_pm_domain *genpd)
133 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
136 * Warn once if an IRQ safe device is attached to a no sleep domain, as
137 * to indicate a suboptimal configuration for PM. For an always on
138 * domain this isn't case, thus don't warn.
140 if (ret && !genpd_is_always_on(genpd))
141 dev_warn_once(dev, "PM domain %s will not be powered off\n",
148 * Get the generic PM domain for a particular struct device.
149 * This validates the struct device pointer, the PM domain pointer,
150 * and checks that the PM domain pointer is a real generic PM domain.
151 * Any failure results in NULL being returned.
153 static struct generic_pm_domain *genpd_lookup_dev(struct device *dev)
155 struct generic_pm_domain *genpd = NULL, *gpd;
157 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
160 mutex_lock(&gpd_list_lock);
161 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
162 if (&gpd->domain == dev->pm_domain) {
167 mutex_unlock(&gpd_list_lock);
173 * This should only be used where we are certain that the pm_domain
174 * attached to the device is a genpd domain.
176 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
178 if (IS_ERR_OR_NULL(dev->pm_domain))
179 return ERR_PTR(-EINVAL);
181 return pd_to_genpd(dev->pm_domain);
184 static int genpd_stop_dev(const struct generic_pm_domain *genpd,
187 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
190 static int genpd_start_dev(const struct generic_pm_domain *genpd,
193 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
196 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
200 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
201 ret = !!atomic_dec_and_test(&genpd->sd_count);
206 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
208 atomic_inc(&genpd->sd_count);
209 smp_mb__after_atomic();
212 #ifdef CONFIG_DEBUG_FS
213 static void genpd_update_accounting(struct generic_pm_domain *genpd)
218 delta = ktime_sub(now, genpd->accounting_time);
221 * If genpd->status is active, it means we are just
222 * out of off and so update the idle time and vice
225 if (genpd->status == GPD_STATE_ACTIVE) {
226 int state_idx = genpd->state_idx;
228 genpd->states[state_idx].idle_time =
229 ktime_add(genpd->states[state_idx].idle_time, delta);
231 genpd->on_time = ktime_add(genpd->on_time, delta);
234 genpd->accounting_time = now;
237 static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
240 static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
242 unsigned int state_idx = genpd->state_idx;
247 if (!genpd->power_on)
251 return genpd->power_on(genpd);
253 time_start = ktime_get();
254 ret = genpd->power_on(genpd);
258 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
259 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
262 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
263 genpd->max_off_time_changed = true;
264 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
265 genpd->name, "on", elapsed_ns);
270 static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
272 unsigned int state_idx = genpd->state_idx;
277 if (!genpd->power_off)
281 return genpd->power_off(genpd);
283 time_start = ktime_get();
284 ret = genpd->power_off(genpd);
288 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
289 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
292 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
293 genpd->max_off_time_changed = true;
294 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
295 genpd->name, "off", elapsed_ns);
301 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
302 * @genpd: PM domain to power off.
304 * Queue up the execution of genpd_power_off() unless it's already been done
307 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
309 queue_work(pm_wq, &genpd->power_off_work);
313 * genpd_power_off - Remove power from a given PM domain.
314 * @genpd: PM domain to power down.
315 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
316 * RPM status of the releated device is in an intermediate state, not yet turned
317 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
318 * be RPM_SUSPENDED, while it tries to power off the PM domain.
320 * If all of the @genpd's devices have been suspended and all of its subdomains
321 * have been powered down, remove power from @genpd.
323 static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
326 struct pm_domain_data *pdd;
327 struct gpd_link *link;
328 unsigned int not_suspended = 0;
331 * Do not try to power off the domain in the following situations:
332 * (1) The domain is already in the "power off" state.
333 * (2) System suspend is in progress.
335 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
339 * Abort power off for the PM domain in the following situations:
340 * (1) The domain is configured as always on.
341 * (2) When the domain has a subdomain being powered on.
343 if (genpd_is_always_on(genpd) || atomic_read(&genpd->sd_count) > 0)
346 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
347 enum pm_qos_flags_status stat;
349 stat = dev_pm_qos_flags(pdd->dev,
350 PM_QOS_FLAG_NO_POWER_OFF
351 | PM_QOS_FLAG_REMOTE_WAKEUP);
352 if (stat > PM_QOS_FLAGS_NONE)
356 * Do not allow PM domain to be powered off, when an IRQ safe
357 * device is part of a non-IRQ safe domain.
359 if (!pm_runtime_suspended(pdd->dev) ||
360 irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
364 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
367 if (genpd->gov && genpd->gov->power_down_ok) {
368 if (!genpd->gov->power_down_ok(&genpd->domain))
372 if (genpd->power_off) {
375 if (atomic_read(&genpd->sd_count) > 0)
379 * If sd_count > 0 at this point, one of the subdomains hasn't
380 * managed to call genpd_power_on() for the master yet after
381 * incrementing it. In that case genpd_power_on() will wait
382 * for us to drop the lock, so we can call .power_off() and let
383 * the genpd_power_on() restore power for us (this shouldn't
384 * happen very often).
386 ret = _genpd_power_off(genpd, true);
391 genpd->status = GPD_STATE_POWER_OFF;
392 genpd_update_accounting(genpd);
394 list_for_each_entry(link, &genpd->slave_links, slave_node) {
395 genpd_sd_counter_dec(link->master);
396 genpd_lock_nested(link->master, depth + 1);
397 genpd_power_off(link->master, false, depth + 1);
398 genpd_unlock(link->master);
405 * genpd_power_on - Restore power to a given PM domain and its masters.
406 * @genpd: PM domain to power up.
407 * @depth: nesting count for lockdep.
409 * Restore power to @genpd and all of its masters so that it is possible to
410 * resume a device belonging to it.
412 static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
414 struct gpd_link *link;
417 if (genpd_status_on(genpd))
421 * The list is guaranteed not to change while the loop below is being
422 * executed, unless one of the masters' .power_on() callbacks fiddles
425 list_for_each_entry(link, &genpd->slave_links, slave_node) {
426 struct generic_pm_domain *master = link->master;
428 genpd_sd_counter_inc(master);
430 genpd_lock_nested(master, depth + 1);
431 ret = genpd_power_on(master, depth + 1);
432 genpd_unlock(master);
435 genpd_sd_counter_dec(master);
440 ret = _genpd_power_on(genpd, true);
444 genpd->status = GPD_STATE_ACTIVE;
445 genpd_update_accounting(genpd);
450 list_for_each_entry_continue_reverse(link,
453 genpd_sd_counter_dec(link->master);
454 genpd_lock_nested(link->master, depth + 1);
455 genpd_power_off(link->master, false, depth + 1);
456 genpd_unlock(link->master);
462 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
463 unsigned long val, void *ptr)
465 struct generic_pm_domain_data *gpd_data;
468 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
469 dev = gpd_data->base.dev;
472 struct generic_pm_domain *genpd;
473 struct pm_domain_data *pdd;
475 spin_lock_irq(&dev->power.lock);
477 pdd = dev->power.subsys_data ?
478 dev->power.subsys_data->domain_data : NULL;
480 to_gpd_data(pdd)->td.constraint_changed = true;
481 genpd = dev_to_genpd(dev);
483 genpd = ERR_PTR(-ENODATA);
486 spin_unlock_irq(&dev->power.lock);
488 if (!IS_ERR(genpd)) {
490 genpd->max_off_time_changed = true;
495 if (!dev || dev->power.ignore_children)
503 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
504 * @work: Work structure used for scheduling the execution of this function.
506 static void genpd_power_off_work_fn(struct work_struct *work)
508 struct generic_pm_domain *genpd;
510 genpd = container_of(work, struct generic_pm_domain, power_off_work);
513 genpd_power_off(genpd, false, 0);
518 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
519 * @dev: Device to handle.
521 static int __genpd_runtime_suspend(struct device *dev)
523 int (*cb)(struct device *__dev);
525 if (dev->type && dev->type->pm)
526 cb = dev->type->pm->runtime_suspend;
527 else if (dev->class && dev->class->pm)
528 cb = dev->class->pm->runtime_suspend;
529 else if (dev->bus && dev->bus->pm)
530 cb = dev->bus->pm->runtime_suspend;
534 if (!cb && dev->driver && dev->driver->pm)
535 cb = dev->driver->pm->runtime_suspend;
537 return cb ? cb(dev) : 0;
541 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
542 * @dev: Device to handle.
544 static int __genpd_runtime_resume(struct device *dev)
546 int (*cb)(struct device *__dev);
548 if (dev->type && dev->type->pm)
549 cb = dev->type->pm->runtime_resume;
550 else if (dev->class && dev->class->pm)
551 cb = dev->class->pm->runtime_resume;
552 else if (dev->bus && dev->bus->pm)
553 cb = dev->bus->pm->runtime_resume;
557 if (!cb && dev->driver && dev->driver->pm)
558 cb = dev->driver->pm->runtime_resume;
560 return cb ? cb(dev) : 0;
564 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
565 * @dev: Device to suspend.
567 * Carry out a runtime suspend of a device under the assumption that its
568 * pm_domain field points to the domain member of an object of type
569 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
571 static int genpd_runtime_suspend(struct device *dev)
573 struct generic_pm_domain *genpd;
574 bool (*suspend_ok)(struct device *__dev);
575 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
576 bool runtime_pm = pm_runtime_enabled(dev);
581 dev_dbg(dev, "%s()\n", __func__);
583 genpd = dev_to_genpd(dev);
588 * A runtime PM centric subsystem/driver may re-use the runtime PM
589 * callbacks for other purposes than runtime PM. In those scenarios
590 * runtime PM is disabled. Under these circumstances, we shall skip
591 * validating/measuring the PM QoS latency.
593 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
594 if (runtime_pm && suspend_ok && !suspend_ok(dev))
597 /* Measure suspend latency. */
600 time_start = ktime_get();
602 ret = __genpd_runtime_suspend(dev);
606 ret = genpd_stop_dev(genpd, dev);
608 __genpd_runtime_resume(dev);
612 /* Update suspend latency value if the measured time exceeds it. */
614 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
615 if (elapsed_ns > td->suspend_latency_ns) {
616 td->suspend_latency_ns = elapsed_ns;
617 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
619 genpd->max_off_time_changed = true;
620 td->constraint_changed = true;
625 * If power.irq_safe is set, this routine may be run with
626 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
628 if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
632 genpd_power_off(genpd, true, 0);
639 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
640 * @dev: Device to resume.
642 * Carry out a runtime resume of a device under the assumption that its
643 * pm_domain field points to the domain member of an object of type
644 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
646 static int genpd_runtime_resume(struct device *dev)
648 struct generic_pm_domain *genpd;
649 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
650 bool runtime_pm = pm_runtime_enabled(dev);
656 dev_dbg(dev, "%s()\n", __func__);
658 genpd = dev_to_genpd(dev);
663 * As we don't power off a non IRQ safe domain, which holds
664 * an IRQ safe device, we don't need to restore power to it.
666 if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
672 ret = genpd_power_on(genpd, 0);
679 /* Measure resume latency. */
681 if (timed && runtime_pm)
682 time_start = ktime_get();
684 ret = genpd_start_dev(genpd, dev);
688 ret = __genpd_runtime_resume(dev);
692 /* Update resume latency value if the measured time exceeds it. */
693 if (timed && runtime_pm) {
694 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
695 if (elapsed_ns > td->resume_latency_ns) {
696 td->resume_latency_ns = elapsed_ns;
697 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
699 genpd->max_off_time_changed = true;
700 td->constraint_changed = true;
707 genpd_stop_dev(genpd, dev);
709 if (!pm_runtime_is_irq_safe(dev) ||
710 (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
712 genpd_power_off(genpd, true, 0);
719 static bool pd_ignore_unused;
720 static int __init pd_ignore_unused_setup(char *__unused)
722 pd_ignore_unused = true;
725 __setup("pd_ignore_unused", pd_ignore_unused_setup);
728 * genpd_power_off_unused - Power off all PM domains with no devices in use.
730 static int __init genpd_power_off_unused(void)
732 struct generic_pm_domain *genpd;
734 if (pd_ignore_unused) {
735 pr_warn("genpd: Not disabling unused power domains\n");
739 mutex_lock(&gpd_list_lock);
741 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
742 genpd_queue_power_off_work(genpd);
744 mutex_unlock(&gpd_list_lock);
748 late_initcall(genpd_power_off_unused);
750 #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
753 * pm_genpd_present - Check if the given PM domain has been initialized.
754 * @genpd: PM domain to check.
756 static bool pm_genpd_present(const struct generic_pm_domain *genpd)
758 const struct generic_pm_domain *gpd;
760 if (IS_ERR_OR_NULL(genpd))
763 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
772 #ifdef CONFIG_PM_SLEEP
774 static bool genpd_dev_active_wakeup(const struct generic_pm_domain *genpd,
777 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
781 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
782 * @genpd: PM domain to power off, if possible.
783 * @use_lock: use the lock.
784 * @depth: nesting count for lockdep.
786 * Check if the given PM domain can be powered off (during system suspend or
787 * hibernation) and do that if so. Also, in that case propagate to its masters.
789 * This function is only called in "noirq" and "syscore" stages of system power
790 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
791 * these cases the lock must be held.
793 static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
796 struct gpd_link *link;
798 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
801 if (genpd->suspended_count != genpd->device_count
802 || atomic_read(&genpd->sd_count) > 0)
805 /* Choose the deepest state when suspending */
806 genpd->state_idx = genpd->state_count - 1;
807 if (_genpd_power_off(genpd, false))
810 genpd->status = GPD_STATE_POWER_OFF;
812 list_for_each_entry(link, &genpd->slave_links, slave_node) {
813 genpd_sd_counter_dec(link->master);
816 genpd_lock_nested(link->master, depth + 1);
818 genpd_sync_power_off(link->master, use_lock, depth + 1);
821 genpd_unlock(link->master);
826 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
827 * @genpd: PM domain to power on.
828 * @use_lock: use the lock.
829 * @depth: nesting count for lockdep.
831 * This function is only called in "noirq" and "syscore" stages of system power
832 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
833 * these cases the lock must be held.
835 static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
838 struct gpd_link *link;
840 if (genpd_status_on(genpd))
843 list_for_each_entry(link, &genpd->slave_links, slave_node) {
844 genpd_sd_counter_inc(link->master);
847 genpd_lock_nested(link->master, depth + 1);
849 genpd_sync_power_on(link->master, use_lock, depth + 1);
852 genpd_unlock(link->master);
855 _genpd_power_on(genpd, false);
857 genpd->status = GPD_STATE_ACTIVE;
861 * resume_needed - Check whether to resume a device before system suspend.
862 * @dev: Device to check.
863 * @genpd: PM domain the device belongs to.
865 * There are two cases in which a device that can wake up the system from sleep
866 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
867 * to wake up the system and it has to remain active for this purpose while the
868 * system is in the sleep state and (2) if the device is not enabled to wake up
869 * the system from sleep states and it generally doesn't generate wakeup signals
870 * by itself (those signals are generated on its behalf by other parts of the
871 * system). In the latter case it may be necessary to reconfigure the device's
872 * wakeup settings during system suspend, because it may have been set up to
873 * signal remote wakeup from the system's working state as needed by runtime PM.
874 * Return 'true' in either of the above cases.
876 static bool resume_needed(struct device *dev,
877 const struct generic_pm_domain *genpd)
881 if (!device_can_wakeup(dev))
884 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
885 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
889 * pm_genpd_prepare - Start power transition of a device in a PM domain.
890 * @dev: Device to start the transition of.
892 * Start a power transition of a device (during a system-wide power transition)
893 * under the assumption that its pm_domain field points to the domain member of
894 * an object of type struct generic_pm_domain representing a PM domain
895 * consisting of I/O devices.
897 static int pm_genpd_prepare(struct device *dev)
899 struct generic_pm_domain *genpd;
902 dev_dbg(dev, "%s()\n", __func__);
904 genpd = dev_to_genpd(dev);
909 * If a wakeup request is pending for the device, it should be woken up
910 * at this point and a system wakeup event should be reported if it's
911 * set up to wake up the system from sleep states.
913 if (resume_needed(dev, genpd))
914 pm_runtime_resume(dev);
918 if (genpd->prepared_count++ == 0)
919 genpd->suspended_count = 0;
923 ret = pm_generic_prepare(dev);
927 genpd->prepared_count--;
932 /* Never return 1, as genpd don't cope with the direct_complete path. */
933 return ret >= 0 ? 0 : ret;
937 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
939 * @dev: Device to suspend.
940 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
942 * Stop the device and remove power from the domain if all devices in it have
945 static int genpd_finish_suspend(struct device *dev, bool poweroff)
947 struct generic_pm_domain *genpd;
950 genpd = dev_to_genpd(dev);
954 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
958 ret = pm_generic_poweroff_noirq(dev);
960 ret = pm_generic_suspend_noirq(dev);
964 if (genpd->dev_ops.stop && genpd->dev_ops.start) {
965 ret = pm_runtime_force_suspend(dev);
971 genpd->suspended_count++;
972 genpd_sync_power_off(genpd, true, 0);
979 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
980 * @dev: Device to suspend.
982 * Stop the device and remove power from the domain if all devices in it have
985 static int pm_genpd_suspend_noirq(struct device *dev)
987 dev_dbg(dev, "%s()\n", __func__);
989 return genpd_finish_suspend(dev, false);
993 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
994 * @dev: Device to resume.
996 * Restore power to the device's PM domain, if necessary, and start the device.
998 static int pm_genpd_resume_noirq(struct device *dev)
1000 struct generic_pm_domain *genpd;
1003 dev_dbg(dev, "%s()\n", __func__);
1005 genpd = dev_to_genpd(dev);
1009 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
1013 genpd_sync_power_on(genpd, true, 0);
1014 genpd->suspended_count--;
1015 genpd_unlock(genpd);
1017 if (genpd->dev_ops.stop && genpd->dev_ops.start)
1018 ret = pm_runtime_force_resume(dev);
1020 ret = pm_generic_resume_noirq(dev);
1028 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1029 * @dev: Device to freeze.
1031 * Carry out a late freeze of a device under the assumption that its
1032 * pm_domain field points to the domain member of an object of type
1033 * struct generic_pm_domain representing a power domain consisting of I/O
1036 static int pm_genpd_freeze_noirq(struct device *dev)
1038 const struct generic_pm_domain *genpd;
1041 dev_dbg(dev, "%s()\n", __func__);
1043 genpd = dev_to_genpd(dev);
1047 ret = pm_generic_freeze_noirq(dev);
1051 if (genpd->dev_ops.stop && genpd->dev_ops.start)
1052 ret = pm_runtime_force_suspend(dev);
1058 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1059 * @dev: Device to thaw.
1061 * Start the device, unless power has been removed from the domain already
1062 * before the system transition.
1064 static int pm_genpd_thaw_noirq(struct device *dev)
1066 const struct generic_pm_domain *genpd;
1069 dev_dbg(dev, "%s()\n", __func__);
1071 genpd = dev_to_genpd(dev);
1075 if (genpd->dev_ops.stop && genpd->dev_ops.start) {
1076 ret = pm_runtime_force_resume(dev);
1081 return pm_generic_thaw_noirq(dev);
1085 * pm_genpd_poweroff_noirq - Completion of hibernation of device in an
1087 * @dev: Device to poweroff.
1089 * Stop the device and remove power from the domain if all devices in it have
1092 static int pm_genpd_poweroff_noirq(struct device *dev)
1094 dev_dbg(dev, "%s()\n", __func__);
1096 return genpd_finish_suspend(dev, true);
1100 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1101 * @dev: Device to resume.
1103 * Make sure the domain will be in the same power state as before the
1104 * hibernation the system is resuming from and start the device if necessary.
1106 static int pm_genpd_restore_noirq(struct device *dev)
1108 struct generic_pm_domain *genpd;
1111 dev_dbg(dev, "%s()\n", __func__);
1113 genpd = dev_to_genpd(dev);
1118 * At this point suspended_count == 0 means we are being run for the
1119 * first time for the given domain in the present cycle.
1122 if (genpd->suspended_count++ == 0)
1124 * The boot kernel might put the domain into arbitrary state,
1125 * so make it appear as powered off to genpd_sync_power_on(),
1126 * so that it tries to power it on in case it was really off.
1128 genpd->status = GPD_STATE_POWER_OFF;
1130 genpd_sync_power_on(genpd, true, 0);
1131 genpd_unlock(genpd);
1133 if (genpd->dev_ops.stop && genpd->dev_ops.start) {
1134 ret = pm_runtime_force_resume(dev);
1139 return pm_generic_restore_noirq(dev);
1143 * pm_genpd_complete - Complete power transition of a device in a power domain.
1144 * @dev: Device to complete the transition of.
1146 * Complete a power transition of a device (during a system-wide power
1147 * transition) under the assumption that its pm_domain field points to the
1148 * domain member of an object of type struct generic_pm_domain representing
1149 * a power domain consisting of I/O devices.
1151 static void pm_genpd_complete(struct device *dev)
1153 struct generic_pm_domain *genpd;
1155 dev_dbg(dev, "%s()\n", __func__);
1157 genpd = dev_to_genpd(dev);
1161 pm_generic_complete(dev);
1165 genpd->prepared_count--;
1166 if (!genpd->prepared_count)
1167 genpd_queue_power_off_work(genpd);
1169 genpd_unlock(genpd);
1173 * genpd_syscore_switch - Switch power during system core suspend or resume.
1174 * @dev: Device that normally is marked as "always on" to switch power for.
1176 * This routine may only be called during the system core (syscore) suspend or
1177 * resume phase for devices whose "always on" flags are set.
1179 static void genpd_syscore_switch(struct device *dev, bool suspend)
1181 struct generic_pm_domain *genpd;
1183 genpd = dev_to_genpd(dev);
1184 if (!pm_genpd_present(genpd))
1188 genpd->suspended_count++;
1189 genpd_sync_power_off(genpd, false, 0);
1191 genpd_sync_power_on(genpd, false, 0);
1192 genpd->suspended_count--;
1196 void pm_genpd_syscore_poweroff(struct device *dev)
1198 genpd_syscore_switch(dev, true);
1200 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1202 void pm_genpd_syscore_poweron(struct device *dev)
1204 genpd_syscore_switch(dev, false);
1206 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1208 #else /* !CONFIG_PM_SLEEP */
1210 #define pm_genpd_prepare NULL
1211 #define pm_genpd_suspend_noirq NULL
1212 #define pm_genpd_resume_noirq NULL
1213 #define pm_genpd_freeze_noirq NULL
1214 #define pm_genpd_thaw_noirq NULL
1215 #define pm_genpd_poweroff_noirq NULL
1216 #define pm_genpd_restore_noirq NULL
1217 #define pm_genpd_complete NULL
1219 #endif /* CONFIG_PM_SLEEP */
1221 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1222 struct generic_pm_domain *genpd,
1223 struct gpd_timing_data *td)
1225 struct generic_pm_domain_data *gpd_data;
1228 ret = dev_pm_get_subsys_data(dev);
1230 return ERR_PTR(ret);
1232 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1241 gpd_data->base.dev = dev;
1242 gpd_data->td.constraint_changed = true;
1243 gpd_data->td.effective_constraint_ns = -1;
1244 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1246 spin_lock_irq(&dev->power.lock);
1248 if (dev->power.subsys_data->domain_data) {
1253 dev->power.subsys_data->domain_data = &gpd_data->base;
1255 spin_unlock_irq(&dev->power.lock);
1260 spin_unlock_irq(&dev->power.lock);
1263 dev_pm_put_subsys_data(dev);
1264 return ERR_PTR(ret);
1267 static void genpd_free_dev_data(struct device *dev,
1268 struct generic_pm_domain_data *gpd_data)
1270 spin_lock_irq(&dev->power.lock);
1272 dev->power.subsys_data->domain_data = NULL;
1274 spin_unlock_irq(&dev->power.lock);
1277 dev_pm_put_subsys_data(dev);
1280 static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1281 struct gpd_timing_data *td)
1283 struct generic_pm_domain_data *gpd_data;
1286 dev_dbg(dev, "%s()\n", __func__);
1288 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1291 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1292 if (IS_ERR(gpd_data))
1293 return PTR_ERR(gpd_data);
1297 if (genpd->prepared_count > 0) {
1302 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1306 dev_pm_domain_set(dev, &genpd->domain);
1308 genpd->device_count++;
1309 genpd->max_off_time_changed = true;
1311 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1314 genpd_unlock(genpd);
1317 genpd_free_dev_data(dev, gpd_data);
1319 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1325 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1326 * @genpd: PM domain to add the device to.
1327 * @dev: Device to be added.
1328 * @td: Set of PM QoS timing parameters to attach to the device.
1330 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1331 struct gpd_timing_data *td)
1335 mutex_lock(&gpd_list_lock);
1336 ret = genpd_add_device(genpd, dev, td);
1337 mutex_unlock(&gpd_list_lock);
1341 EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
1343 static int genpd_remove_device(struct generic_pm_domain *genpd,
1346 struct generic_pm_domain_data *gpd_data;
1347 struct pm_domain_data *pdd;
1350 dev_dbg(dev, "%s()\n", __func__);
1352 pdd = dev->power.subsys_data->domain_data;
1353 gpd_data = to_gpd_data(pdd);
1354 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1358 if (genpd->prepared_count > 0) {
1363 genpd->device_count--;
1364 genpd->max_off_time_changed = true;
1366 if (genpd->detach_dev)
1367 genpd->detach_dev(genpd, dev);
1369 dev_pm_domain_set(dev, NULL);
1371 list_del_init(&pdd->list_node);
1373 genpd_unlock(genpd);
1375 genpd_free_dev_data(dev, gpd_data);
1380 genpd_unlock(genpd);
1381 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1387 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1388 * @genpd: PM domain to remove the device from.
1389 * @dev: Device to be removed.
1391 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1394 if (!genpd || genpd != genpd_lookup_dev(dev))
1397 return genpd_remove_device(genpd, dev);
1399 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1401 static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1402 struct generic_pm_domain *subdomain)
1404 struct gpd_link *link, *itr;
1407 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1408 || genpd == subdomain)
1412 * If the domain can be powered on/off in an IRQ safe
1413 * context, ensure that the subdomain can also be
1414 * powered on/off in that context.
1416 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1417 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1418 genpd->name, subdomain->name);
1422 link = kzalloc(sizeof(*link), GFP_KERNEL);
1426 genpd_lock(subdomain);
1427 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1429 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1434 list_for_each_entry(itr, &genpd->master_links, master_node) {
1435 if (itr->slave == subdomain && itr->master == genpd) {
1441 link->master = genpd;
1442 list_add_tail(&link->master_node, &genpd->master_links);
1443 link->slave = subdomain;
1444 list_add_tail(&link->slave_node, &subdomain->slave_links);
1445 if (genpd_status_on(subdomain))
1446 genpd_sd_counter_inc(genpd);
1449 genpd_unlock(genpd);
1450 genpd_unlock(subdomain);
1457 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1458 * @genpd: Master PM domain to add the subdomain to.
1459 * @subdomain: Subdomain to be added.
1461 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1462 struct generic_pm_domain *subdomain)
1466 mutex_lock(&gpd_list_lock);
1467 ret = genpd_add_subdomain(genpd, subdomain);
1468 mutex_unlock(&gpd_list_lock);
1472 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1475 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1476 * @genpd: Master PM domain to remove the subdomain from.
1477 * @subdomain: Subdomain to be removed.
1479 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1480 struct generic_pm_domain *subdomain)
1482 struct gpd_link *l, *link;
1485 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1488 genpd_lock(subdomain);
1489 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1491 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1492 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1498 list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
1499 if (link->slave != subdomain)
1502 list_del(&link->master_node);
1503 list_del(&link->slave_node);
1505 if (genpd_status_on(subdomain))
1506 genpd_sd_counter_dec(genpd);
1513 genpd_unlock(genpd);
1514 genpd_unlock(subdomain);
1518 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1520 static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1522 struct genpd_power_state *state;
1524 state = kzalloc(sizeof(*state), GFP_KERNEL);
1528 genpd->states = state;
1529 genpd->state_count = 1;
1530 genpd->free = state;
1535 static void genpd_lock_init(struct generic_pm_domain *genpd)
1537 if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1538 spin_lock_init(&genpd->slock);
1539 genpd->lock_ops = &genpd_spin_ops;
1541 mutex_init(&genpd->mlock);
1542 genpd->lock_ops = &genpd_mtx_ops;
1547 * pm_genpd_init - Initialize a generic I/O PM domain object.
1548 * @genpd: PM domain object to initialize.
1549 * @gov: PM domain governor to associate with the domain (may be NULL).
1550 * @is_off: Initial value of the domain's power_is_off field.
1552 * Returns 0 on successful initialization, else a negative error code.
1554 int pm_genpd_init(struct generic_pm_domain *genpd,
1555 struct dev_power_governor *gov, bool is_off)
1559 if (IS_ERR_OR_NULL(genpd))
1562 INIT_LIST_HEAD(&genpd->master_links);
1563 INIT_LIST_HEAD(&genpd->slave_links);
1564 INIT_LIST_HEAD(&genpd->dev_list);
1565 genpd_lock_init(genpd);
1567 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1568 atomic_set(&genpd->sd_count, 0);
1569 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1570 genpd->device_count = 0;
1571 genpd->max_off_time_ns = -1;
1572 genpd->max_off_time_changed = true;
1573 genpd->provider = NULL;
1574 genpd->has_provider = false;
1575 genpd->accounting_time = ktime_get();
1576 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1577 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
1578 genpd->domain.ops.prepare = pm_genpd_prepare;
1579 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1580 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1581 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1582 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1583 genpd->domain.ops.poweroff_noirq = pm_genpd_poweroff_noirq;
1584 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1585 genpd->domain.ops.complete = pm_genpd_complete;
1587 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1588 genpd->dev_ops.stop = pm_clk_suspend;
1589 genpd->dev_ops.start = pm_clk_resume;
1592 /* Always-on domains must be powered on at initialization. */
1593 if (genpd_is_always_on(genpd) && !genpd_status_on(genpd))
1596 /* Use only one "off" state if there were no states declared */
1597 if (genpd->state_count == 0) {
1598 ret = genpd_set_default_power_state(genpd);
1603 mutex_lock(&gpd_list_lock);
1604 list_add(&genpd->gpd_list_node, &gpd_list);
1605 mutex_unlock(&gpd_list_lock);
1609 EXPORT_SYMBOL_GPL(pm_genpd_init);
1611 static int genpd_remove(struct generic_pm_domain *genpd)
1613 struct gpd_link *l, *link;
1615 if (IS_ERR_OR_NULL(genpd))
1620 if (genpd->has_provider) {
1621 genpd_unlock(genpd);
1622 pr_err("Provider present, unable to remove %s\n", genpd->name);
1626 if (!list_empty(&genpd->master_links) || genpd->device_count) {
1627 genpd_unlock(genpd);
1628 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1632 list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
1633 list_del(&link->master_node);
1634 list_del(&link->slave_node);
1638 list_del(&genpd->gpd_list_node);
1639 genpd_unlock(genpd);
1640 cancel_work_sync(&genpd->power_off_work);
1642 pr_debug("%s: removed %s\n", __func__, genpd->name);
1648 * pm_genpd_remove - Remove a generic I/O PM domain
1649 * @genpd: Pointer to PM domain that is to be removed.
1651 * To remove the PM domain, this function:
1652 * - Removes the PM domain as a subdomain to any parent domains,
1654 * - Removes the PM domain from the list of registered PM domains.
1656 * The PM domain will only be removed, if the associated provider has
1657 * been removed, it is not a parent to any other PM domain and has no
1658 * devices associated with it.
1660 int pm_genpd_remove(struct generic_pm_domain *genpd)
1664 mutex_lock(&gpd_list_lock);
1665 ret = genpd_remove(genpd);
1666 mutex_unlock(&gpd_list_lock);
1670 EXPORT_SYMBOL_GPL(pm_genpd_remove);
1672 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1675 * Device Tree based PM domain providers.
1677 * The code below implements generic device tree based PM domain providers that
1678 * bind device tree nodes with generic PM domains registered in the system.
1680 * Any driver that registers generic PM domains and needs to support binding of
1681 * devices to these domains is supposed to register a PM domain provider, which
1682 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1684 * Two simple mapping functions have been provided for convenience:
1685 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1686 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1691 * struct of_genpd_provider - PM domain provider registration structure
1692 * @link: Entry in global list of PM domain providers
1693 * @node: Pointer to device tree node of PM domain provider
1694 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1696 * @data: context pointer to be passed into @xlate callback
1698 struct of_genpd_provider {
1699 struct list_head link;
1700 struct device_node *node;
1701 genpd_xlate_t xlate;
1705 /* List of registered PM domain providers. */
1706 static LIST_HEAD(of_genpd_providers);
1707 /* Mutex to protect the list above. */
1708 static DEFINE_MUTEX(of_genpd_mutex);
1711 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1712 * @genpdspec: OF phandle args to map into a PM domain
1713 * @data: xlate function private data - pointer to struct generic_pm_domain
1715 * This is a generic xlate function that can be used to model PM domains that
1716 * have their own device tree nodes. The private data of xlate function needs
1717 * to be a valid pointer to struct generic_pm_domain.
1719 static struct generic_pm_domain *genpd_xlate_simple(
1720 struct of_phandle_args *genpdspec,
1727 * genpd_xlate_onecell() - Xlate function using a single index.
1728 * @genpdspec: OF phandle args to map into a PM domain
1729 * @data: xlate function private data - pointer to struct genpd_onecell_data
1731 * This is a generic xlate function that can be used to model simple PM domain
1732 * controllers that have one device tree node and provide multiple PM domains.
1733 * A single cell is used as an index into an array of PM domains specified in
1734 * the genpd_onecell_data struct when registering the provider.
1736 static struct generic_pm_domain *genpd_xlate_onecell(
1737 struct of_phandle_args *genpdspec,
1740 struct genpd_onecell_data *genpd_data = data;
1741 unsigned int idx = genpdspec->args[0];
1743 if (genpdspec->args_count != 1)
1744 return ERR_PTR(-EINVAL);
1746 if (idx >= genpd_data->num_domains) {
1747 pr_err("%s: invalid domain index %u\n", __func__, idx);
1748 return ERR_PTR(-EINVAL);
1751 if (!genpd_data->domains[idx])
1752 return ERR_PTR(-ENOENT);
1754 return genpd_data->domains[idx];
1758 * genpd_add_provider() - Register a PM domain provider for a node
1759 * @np: Device node pointer associated with the PM domain provider.
1760 * @xlate: Callback for decoding PM domain from phandle arguments.
1761 * @data: Context pointer for @xlate callback.
1763 static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1766 struct of_genpd_provider *cp;
1768 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1772 cp->node = of_node_get(np);
1776 mutex_lock(&of_genpd_mutex);
1777 list_add(&cp->link, &of_genpd_providers);
1778 mutex_unlock(&of_genpd_mutex);
1779 pr_debug("Added domain provider from %pOF\n", np);
1785 * of_genpd_add_provider_simple() - Register a simple PM domain provider
1786 * @np: Device node pointer associated with the PM domain provider.
1787 * @genpd: Pointer to PM domain associated with the PM domain provider.
1789 int of_genpd_add_provider_simple(struct device_node *np,
1790 struct generic_pm_domain *genpd)
1797 mutex_lock(&gpd_list_lock);
1799 if (pm_genpd_present(genpd)) {
1800 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
1802 genpd->provider = &np->fwnode;
1803 genpd->has_provider = true;
1807 mutex_unlock(&gpd_list_lock);
1811 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
1814 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
1815 * @np: Device node pointer associated with the PM domain provider.
1816 * @data: Pointer to the data associated with the PM domain provider.
1818 int of_genpd_add_provider_onecell(struct device_node *np,
1819 struct genpd_onecell_data *data)
1827 mutex_lock(&gpd_list_lock);
1830 data->xlate = genpd_xlate_onecell;
1832 for (i = 0; i < data->num_domains; i++) {
1833 if (!data->domains[i])
1835 if (!pm_genpd_present(data->domains[i]))
1838 data->domains[i]->provider = &np->fwnode;
1839 data->domains[i]->has_provider = true;
1842 ret = genpd_add_provider(np, data->xlate, data);
1846 mutex_unlock(&gpd_list_lock);
1852 if (!data->domains[i])
1854 data->domains[i]->provider = NULL;
1855 data->domains[i]->has_provider = false;
1858 mutex_unlock(&gpd_list_lock);
1862 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
1865 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1866 * @np: Device node pointer associated with the PM domain provider
1868 void of_genpd_del_provider(struct device_node *np)
1870 struct of_genpd_provider *cp, *tmp;
1871 struct generic_pm_domain *gpd;
1873 mutex_lock(&gpd_list_lock);
1874 mutex_lock(&of_genpd_mutex);
1875 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
1876 if (cp->node == np) {
1878 * For each PM domain associated with the
1879 * provider, set the 'has_provider' to false
1880 * so that the PM domain can be safely removed.
1882 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
1883 if (gpd->provider == &np->fwnode)
1884 gpd->has_provider = false;
1886 list_del(&cp->link);
1887 of_node_put(cp->node);
1892 mutex_unlock(&of_genpd_mutex);
1893 mutex_unlock(&gpd_list_lock);
1895 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1898 * genpd_get_from_provider() - Look-up PM domain
1899 * @genpdspec: OF phandle args to use for look-up
1901 * Looks for a PM domain provider under the node specified by @genpdspec and if
1902 * found, uses xlate function of the provider to map phandle args to a PM
1905 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1908 static struct generic_pm_domain *genpd_get_from_provider(
1909 struct of_phandle_args *genpdspec)
1911 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1912 struct of_genpd_provider *provider;
1915 return ERR_PTR(-EINVAL);
1917 mutex_lock(&of_genpd_mutex);
1919 /* Check if we have such a provider in our array */
1920 list_for_each_entry(provider, &of_genpd_providers, link) {
1921 if (provider->node == genpdspec->np)
1922 genpd = provider->xlate(genpdspec, provider->data);
1927 mutex_unlock(&of_genpd_mutex);
1933 * of_genpd_add_device() - Add a device to an I/O PM domain
1934 * @genpdspec: OF phandle args to use for look-up PM domain
1935 * @dev: Device to be added.
1937 * Looks-up an I/O PM domain based upon phandle args provided and adds
1938 * the device to the PM domain. Returns a negative error code on failure.
1940 int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
1942 struct generic_pm_domain *genpd;
1945 mutex_lock(&gpd_list_lock);
1947 genpd = genpd_get_from_provider(genpdspec);
1948 if (IS_ERR(genpd)) {
1949 ret = PTR_ERR(genpd);
1953 ret = genpd_add_device(genpd, dev, NULL);
1956 mutex_unlock(&gpd_list_lock);
1960 EXPORT_SYMBOL_GPL(of_genpd_add_device);
1963 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1964 * @parent_spec: OF phandle args to use for parent PM domain look-up
1965 * @subdomain_spec: OF phandle args to use for subdomain look-up
1967 * Looks-up a parent PM domain and subdomain based upon phandle args
1968 * provided and adds the subdomain to the parent PM domain. Returns a
1969 * negative error code on failure.
1971 int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
1972 struct of_phandle_args *subdomain_spec)
1974 struct generic_pm_domain *parent, *subdomain;
1977 mutex_lock(&gpd_list_lock);
1979 parent = genpd_get_from_provider(parent_spec);
1980 if (IS_ERR(parent)) {
1981 ret = PTR_ERR(parent);
1985 subdomain = genpd_get_from_provider(subdomain_spec);
1986 if (IS_ERR(subdomain)) {
1987 ret = PTR_ERR(subdomain);
1991 ret = genpd_add_subdomain(parent, subdomain);
1994 mutex_unlock(&gpd_list_lock);
1998 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2001 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2002 * @provider: Pointer to device structure associated with provider
2004 * Find the last PM domain that was added by a particular provider and
2005 * remove this PM domain from the list of PM domains. The provider is
2006 * identified by the 'provider' device structure that is passed. The PM
2007 * domain will only be removed, if the provider associated with domain
2010 * Returns a valid pointer to struct generic_pm_domain on success or
2011 * ERR_PTR() on failure.
2013 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2015 struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2018 if (IS_ERR_OR_NULL(np))
2019 return ERR_PTR(-EINVAL);
2021 mutex_lock(&gpd_list_lock);
2022 list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2023 if (gpd->provider == &np->fwnode) {
2024 ret = genpd_remove(gpd);
2025 genpd = ret ? ERR_PTR(ret) : gpd;
2029 mutex_unlock(&gpd_list_lock);
2033 EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2036 * genpd_dev_pm_detach - Detach a device from its PM domain.
2037 * @dev: Device to detach.
2038 * @power_off: Currently not used
2040 * Try to locate a corresponding generic PM domain, which the device was
2041 * attached to previously. If such is found, the device is detached from it.
2043 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2045 struct generic_pm_domain *pd;
2049 pd = dev_to_genpd(dev);
2053 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2055 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2056 ret = genpd_remove_device(pd, dev);
2065 dev_err(dev, "failed to remove from PM domain %s: %d",
2070 /* Check if PM domain can be powered off after removing this device. */
2071 genpd_queue_power_off_work(pd);
2074 static void genpd_dev_pm_sync(struct device *dev)
2076 struct generic_pm_domain *pd;
2078 pd = dev_to_genpd(dev);
2082 genpd_queue_power_off_work(pd);
2086 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2087 * @dev: Device to attach.
2089 * Parse device's OF node to find a PM domain specifier. If such is found,
2090 * attaches the device to retrieved pm_domain ops.
2092 * Both generic and legacy Samsung-specific DT bindings are supported to keep
2093 * backwards compatibility with existing DTBs.
2095 * Returns 0 on successfully attached PM domain or negative error code. Note
2096 * that if a power-domain exists for the device, but it cannot be found or
2097 * turned on, then return -EPROBE_DEFER to ensure that the device is not
2098 * probed and to re-try again later.
2100 int genpd_dev_pm_attach(struct device *dev)
2102 struct of_phandle_args pd_args;
2103 struct generic_pm_domain *pd;
2113 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2114 "#power-domain-cells", 0, &pd_args);
2120 * Try legacy Samsung-specific bindings
2121 * (for backwards compatibility of DT ABI)
2123 pd_args.args_count = 0;
2124 pd_args.np = of_parse_phandle(dev->of_node,
2125 "samsung,power-domain", 0);
2130 mutex_lock(&gpd_list_lock);
2131 pd = genpd_get_from_provider(&pd_args);
2132 of_node_put(pd_args.np);
2134 mutex_unlock(&gpd_list_lock);
2135 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2136 __func__, PTR_ERR(pd));
2137 return -EPROBE_DEFER;
2140 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2142 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2143 ret = genpd_add_device(pd, dev, NULL);
2150 mutex_unlock(&gpd_list_lock);
2153 if (ret != -EPROBE_DEFER)
2154 dev_err(dev, "failed to add to PM domain %s: %d",
2159 dev->pm_domain->detach = genpd_dev_pm_detach;
2160 dev->pm_domain->sync = genpd_dev_pm_sync;
2163 ret = genpd_power_on(pd, 0);
2167 genpd_remove_device(pd, dev);
2169 return ret ? -EPROBE_DEFER : 0;
2171 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2173 static const struct of_device_id idle_state_match[] = {
2174 { .compatible = "domain-idle-state", },
2178 static int genpd_parse_state(struct genpd_power_state *genpd_state,
2179 struct device_node *state_node)
2183 u32 entry_latency, exit_latency;
2185 err = of_property_read_u32(state_node, "entry-latency-us",
2188 pr_debug(" * %pOF missing entry-latency-us property\n",
2193 err = of_property_read_u32(state_node, "exit-latency-us",
2196 pr_debug(" * %pOF missing exit-latency-us property\n",
2201 err = of_property_read_u32(state_node, "min-residency-us", &residency);
2203 genpd_state->residency_ns = 1000 * residency;
2205 genpd_state->power_on_latency_ns = 1000 * exit_latency;
2206 genpd_state->power_off_latency_ns = 1000 * entry_latency;
2207 genpd_state->fwnode = &state_node->fwnode;
2212 static int genpd_iterate_idle_states(struct device_node *dn,
2213 struct genpd_power_state *states)
2216 struct of_phandle_iterator it;
2217 struct device_node *np;
2220 ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2224 /* Loop over the phandles until all the requested entry is found */
2225 of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
2227 if (!of_match_node(idle_state_match, np))
2230 ret = genpd_parse_state(&states[i], np);
2232 pr_err("Parsing idle state node %pOF failed with err %d\n",
2245 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2247 * @dn: The genpd device node
2248 * @states: The pointer to which the state array will be saved.
2249 * @n: The count of elements in the array returned from this function.
2251 * Returns the device states parsed from the OF node. The memory for the states
2252 * is allocated by this function and is the responsibility of the caller to
2253 * free the memory after use. If no domain idle states is found it returns
2254 * -EINVAL and in case of errors, a negative error code.
2256 int of_genpd_parse_idle_states(struct device_node *dn,
2257 struct genpd_power_state **states, int *n)
2259 struct genpd_power_state *st;
2262 ret = genpd_iterate_idle_states(dn, NULL);
2264 return ret < 0 ? ret : -EINVAL;
2266 st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
2270 ret = genpd_iterate_idle_states(dn, st);
2273 return ret < 0 ? ret : -EINVAL;
2281 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2283 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2286 /*** debugfs support ***/
2288 #ifdef CONFIG_DEBUG_FS
2289 #include <linux/pm.h>
2290 #include <linux/device.h>
2291 #include <linux/debugfs.h>
2292 #include <linux/seq_file.h>
2293 #include <linux/init.h>
2294 #include <linux/kobject.h>
2295 static struct dentry *pm_genpd_debugfs_dir;
2298 * TODO: This function is a slightly modified version of rtpm_status_show
2299 * from sysfs.c, so generalize it.
2301 static void rtpm_status_str(struct seq_file *s, struct device *dev)
2303 static const char * const status_lookup[] = {
2304 [RPM_ACTIVE] = "active",
2305 [RPM_RESUMING] = "resuming",
2306 [RPM_SUSPENDED] = "suspended",
2307 [RPM_SUSPENDING] = "suspending"
2311 if (dev->power.runtime_error)
2313 else if (dev->power.disable_depth)
2315 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2316 p = status_lookup[dev->power.runtime_status];
2323 static int pm_genpd_summary_one(struct seq_file *s,
2324 struct generic_pm_domain *genpd)
2326 static const char * const status_lookup[] = {
2327 [GPD_STATE_ACTIVE] = "on",
2328 [GPD_STATE_POWER_OFF] = "off"
2330 struct pm_domain_data *pm_data;
2331 const char *kobj_path;
2332 struct gpd_link *link;
2336 ret = genpd_lock_interruptible(genpd);
2338 return -ERESTARTSYS;
2340 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2342 if (!genpd_status_on(genpd))
2343 snprintf(state, sizeof(state), "%s-%u",
2344 status_lookup[genpd->status], genpd->state_idx);
2346 snprintf(state, sizeof(state), "%s",
2347 status_lookup[genpd->status]);
2348 seq_printf(s, "%-30s %-15s ", genpd->name, state);
2351 * Modifications on the list require holding locks on both
2352 * master and slave, so we are safe.
2353 * Also genpd->name is immutable.
2355 list_for_each_entry(link, &genpd->master_links, master_node) {
2356 seq_printf(s, "%s", link->slave->name);
2357 if (!list_is_last(&link->master_node, &genpd->master_links))
2361 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2362 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2363 genpd_is_irq_safe(genpd) ?
2364 GFP_ATOMIC : GFP_KERNEL);
2365 if (kobj_path == NULL)
2368 seq_printf(s, "\n %-50s ", kobj_path);
2369 rtpm_status_str(s, pm_data->dev);
2375 genpd_unlock(genpd);
2380 static int genpd_summary_show(struct seq_file *s, void *data)
2382 struct generic_pm_domain *genpd;
2385 seq_puts(s, "domain status slaves\n");
2386 seq_puts(s, " /device runtime status\n");
2387 seq_puts(s, "----------------------------------------------------------------------\n");
2389 ret = mutex_lock_interruptible(&gpd_list_lock);
2391 return -ERESTARTSYS;
2393 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2394 ret = pm_genpd_summary_one(s, genpd);
2398 mutex_unlock(&gpd_list_lock);
2403 static int genpd_status_show(struct seq_file *s, void *data)
2405 static const char * const status_lookup[] = {
2406 [GPD_STATE_ACTIVE] = "on",
2407 [GPD_STATE_POWER_OFF] = "off"
2410 struct generic_pm_domain *genpd = s->private;
2413 ret = genpd_lock_interruptible(genpd);
2415 return -ERESTARTSYS;
2417 if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
2420 if (genpd->status == GPD_STATE_POWER_OFF)
2421 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
2424 seq_printf(s, "%s\n", status_lookup[genpd->status]);
2426 genpd_unlock(genpd);
2430 static int genpd_sub_domains_show(struct seq_file *s, void *data)
2432 struct generic_pm_domain *genpd = s->private;
2433 struct gpd_link *link;
2436 ret = genpd_lock_interruptible(genpd);
2438 return -ERESTARTSYS;
2440 list_for_each_entry(link, &genpd->master_links, master_node)
2441 seq_printf(s, "%s\n", link->slave->name);
2443 genpd_unlock(genpd);
2447 static int genpd_idle_states_show(struct seq_file *s, void *data)
2449 struct generic_pm_domain *genpd = s->private;
2453 ret = genpd_lock_interruptible(genpd);
2455 return -ERESTARTSYS;
2457 seq_puts(s, "State Time Spent(ms)\n");
2459 for (i = 0; i < genpd->state_count; i++) {
2463 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2464 (genpd->state_idx == i))
2465 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2467 msecs = ktime_to_ms(
2468 ktime_add(genpd->states[i].idle_time, delta));
2469 seq_printf(s, "S%-13i %lld\n", i, msecs);
2472 genpd_unlock(genpd);
2476 static int genpd_active_time_show(struct seq_file *s, void *data)
2478 struct generic_pm_domain *genpd = s->private;
2482 ret = genpd_lock_interruptible(genpd);
2484 return -ERESTARTSYS;
2486 if (genpd->status == GPD_STATE_ACTIVE)
2487 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2489 seq_printf(s, "%lld ms\n", ktime_to_ms(
2490 ktime_add(genpd->on_time, delta)));
2492 genpd_unlock(genpd);
2496 static int genpd_total_idle_time_show(struct seq_file *s, void *data)
2498 struct generic_pm_domain *genpd = s->private;
2499 ktime_t delta = 0, total = 0;
2503 ret = genpd_lock_interruptible(genpd);
2505 return -ERESTARTSYS;
2507 for (i = 0; i < genpd->state_count; i++) {
2509 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2510 (genpd->state_idx == i))
2511 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2513 total = ktime_add(total, genpd->states[i].idle_time);
2515 total = ktime_add(total, delta);
2517 seq_printf(s, "%lld ms\n", ktime_to_ms(total));
2519 genpd_unlock(genpd);
2524 static int genpd_devices_show(struct seq_file *s, void *data)
2526 struct generic_pm_domain *genpd = s->private;
2527 struct pm_domain_data *pm_data;
2528 const char *kobj_path;
2531 ret = genpd_lock_interruptible(genpd);
2533 return -ERESTARTSYS;
2535 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2536 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2537 genpd_is_irq_safe(genpd) ?
2538 GFP_ATOMIC : GFP_KERNEL);
2539 if (kobj_path == NULL)
2542 seq_printf(s, "%s\n", kobj_path);
2546 genpd_unlock(genpd);
2550 #define define_genpd_open_function(name) \
2551 static int genpd_##name##_open(struct inode *inode, struct file *file) \
2553 return single_open(file, genpd_##name##_show, inode->i_private); \
2556 define_genpd_open_function(summary);
2557 define_genpd_open_function(status);
2558 define_genpd_open_function(sub_domains);
2559 define_genpd_open_function(idle_states);
2560 define_genpd_open_function(active_time);
2561 define_genpd_open_function(total_idle_time);
2562 define_genpd_open_function(devices);
2564 #define define_genpd_debugfs_fops(name) \
2565 static const struct file_operations genpd_##name##_fops = { \
2566 .open = genpd_##name##_open, \
2568 .llseek = seq_lseek, \
2569 .release = single_release, \
2572 define_genpd_debugfs_fops(summary);
2573 define_genpd_debugfs_fops(status);
2574 define_genpd_debugfs_fops(sub_domains);
2575 define_genpd_debugfs_fops(idle_states);
2576 define_genpd_debugfs_fops(active_time);
2577 define_genpd_debugfs_fops(total_idle_time);
2578 define_genpd_debugfs_fops(devices);
2580 static int __init pm_genpd_debug_init(void)
2583 struct generic_pm_domain *genpd;
2585 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2587 if (!pm_genpd_debugfs_dir)
2590 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
2591 pm_genpd_debugfs_dir, NULL, &genpd_summary_fops);
2595 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2596 d = debugfs_create_dir(genpd->name, pm_genpd_debugfs_dir);
2600 debugfs_create_file("current_state", 0444,
2601 d, genpd, &genpd_status_fops);
2602 debugfs_create_file("sub_domains", 0444,
2603 d, genpd, &genpd_sub_domains_fops);
2604 debugfs_create_file("idle_states", 0444,
2605 d, genpd, &genpd_idle_states_fops);
2606 debugfs_create_file("active_time", 0444,
2607 d, genpd, &genpd_active_time_fops);
2608 debugfs_create_file("total_idle_time", 0444,
2609 d, genpd, &genpd_total_idle_time_fops);
2610 debugfs_create_file("devices", 0444,
2611 d, genpd, &genpd_devices_fops);
2616 late_initcall(pm_genpd_debug_init);
2618 static void __exit pm_genpd_debug_exit(void)
2620 debugfs_remove_recursive(pm_genpd_debugfs_dir);
2622 __exitcall(pm_genpd_debug_exit);
2623 #endif /* CONFIG_DEBUG_FS */