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 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(struct generic_pm_domain *genpd, struct device *dev)
186 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
189 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
191 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
194 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
198 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
199 ret = !!atomic_dec_and_test(&genpd->sd_count);
204 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
206 atomic_inc(&genpd->sd_count);
207 smp_mb__after_atomic();
210 static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
212 unsigned int state_idx = genpd->state_idx;
217 if (!genpd->power_on)
221 return genpd->power_on(genpd);
223 time_start = ktime_get();
224 ret = genpd->power_on(genpd);
228 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
229 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
232 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
233 genpd->max_off_time_changed = true;
234 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
235 genpd->name, "on", elapsed_ns);
240 static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
242 unsigned int state_idx = genpd->state_idx;
247 if (!genpd->power_off)
251 return genpd->power_off(genpd);
253 time_start = ktime_get();
254 ret = genpd->power_off(genpd);
258 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
259 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
262 genpd->states[state_idx].power_off_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, "off", elapsed_ns);
271 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
272 * @genpd: PM domain to power off.
274 * Queue up the execution of genpd_power_off() unless it's already been done
277 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
279 queue_work(pm_wq, &genpd->power_off_work);
283 * genpd_power_off - Remove power from a given PM domain.
284 * @genpd: PM domain to power down.
285 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
286 * RPM status of the releated device is in an intermediate state, not yet turned
287 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
288 * be RPM_SUSPENDED, while it tries to power off the PM domain.
290 * If all of the @genpd's devices have been suspended and all of its subdomains
291 * have been powered down, remove power from @genpd.
293 static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
296 struct pm_domain_data *pdd;
297 struct gpd_link *link;
298 unsigned int not_suspended = 0;
301 * Do not try to power off the domain in the following situations:
302 * (1) The domain is already in the "power off" state.
303 * (2) System suspend is in progress.
305 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
309 * Abort power off for the PM domain in the following situations:
310 * (1) The domain is configured as always on.
311 * (2) When the domain has a subdomain being powered on.
313 if (genpd_is_always_on(genpd) || atomic_read(&genpd->sd_count) > 0)
316 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
317 enum pm_qos_flags_status stat;
319 stat = dev_pm_qos_flags(pdd->dev,
320 PM_QOS_FLAG_NO_POWER_OFF
321 | PM_QOS_FLAG_REMOTE_WAKEUP);
322 if (stat > PM_QOS_FLAGS_NONE)
326 * Do not allow PM domain to be powered off, when an IRQ safe
327 * device is part of a non-IRQ safe domain.
329 if (!pm_runtime_suspended(pdd->dev) ||
330 irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
334 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
337 if (genpd->gov && genpd->gov->power_down_ok) {
338 if (!genpd->gov->power_down_ok(&genpd->domain))
342 if (genpd->power_off) {
345 if (atomic_read(&genpd->sd_count) > 0)
349 * If sd_count > 0 at this point, one of the subdomains hasn't
350 * managed to call genpd_power_on() for the master yet after
351 * incrementing it. In that case genpd_power_on() will wait
352 * for us to drop the lock, so we can call .power_off() and let
353 * the genpd_power_on() restore power for us (this shouldn't
354 * happen very often).
356 ret = _genpd_power_off(genpd, true);
361 genpd->status = GPD_STATE_POWER_OFF;
363 list_for_each_entry(link, &genpd->slave_links, slave_node) {
364 genpd_sd_counter_dec(link->master);
365 genpd_lock_nested(link->master, depth + 1);
366 genpd_power_off(link->master, false, depth + 1);
367 genpd_unlock(link->master);
374 * genpd_power_on - Restore power to a given PM domain and its masters.
375 * @genpd: PM domain to power up.
376 * @depth: nesting count for lockdep.
378 * Restore power to @genpd and all of its masters so that it is possible to
379 * resume a device belonging to it.
381 static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
383 struct gpd_link *link;
386 if (genpd_status_on(genpd))
390 * The list is guaranteed not to change while the loop below is being
391 * executed, unless one of the masters' .power_on() callbacks fiddles
394 list_for_each_entry(link, &genpd->slave_links, slave_node) {
395 struct generic_pm_domain *master = link->master;
397 genpd_sd_counter_inc(master);
399 genpd_lock_nested(master, depth + 1);
400 ret = genpd_power_on(master, depth + 1);
401 genpd_unlock(master);
404 genpd_sd_counter_dec(master);
409 ret = _genpd_power_on(genpd, true);
413 genpd->status = GPD_STATE_ACTIVE;
417 list_for_each_entry_continue_reverse(link,
420 genpd_sd_counter_dec(link->master);
421 genpd_lock_nested(link->master, depth + 1);
422 genpd_power_off(link->master, false, depth + 1);
423 genpd_unlock(link->master);
429 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
430 unsigned long val, void *ptr)
432 struct generic_pm_domain_data *gpd_data;
435 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
436 dev = gpd_data->base.dev;
439 struct generic_pm_domain *genpd;
440 struct pm_domain_data *pdd;
442 spin_lock_irq(&dev->power.lock);
444 pdd = dev->power.subsys_data ?
445 dev->power.subsys_data->domain_data : NULL;
446 if (pdd && pdd->dev) {
447 to_gpd_data(pdd)->td.constraint_changed = true;
448 genpd = dev_to_genpd(dev);
450 genpd = ERR_PTR(-ENODATA);
453 spin_unlock_irq(&dev->power.lock);
455 if (!IS_ERR(genpd)) {
457 genpd->max_off_time_changed = true;
462 if (!dev || dev->power.ignore_children)
470 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
471 * @work: Work structure used for scheduling the execution of this function.
473 static void genpd_power_off_work_fn(struct work_struct *work)
475 struct generic_pm_domain *genpd;
477 genpd = container_of(work, struct generic_pm_domain, power_off_work);
480 genpd_power_off(genpd, false, 0);
485 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
486 * @dev: Device to handle.
488 static int __genpd_runtime_suspend(struct device *dev)
490 int (*cb)(struct device *__dev);
492 if (dev->type && dev->type->pm)
493 cb = dev->type->pm->runtime_suspend;
494 else if (dev->class && dev->class->pm)
495 cb = dev->class->pm->runtime_suspend;
496 else if (dev->bus && dev->bus->pm)
497 cb = dev->bus->pm->runtime_suspend;
501 if (!cb && dev->driver && dev->driver->pm)
502 cb = dev->driver->pm->runtime_suspend;
504 return cb ? cb(dev) : 0;
508 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
509 * @dev: Device to handle.
511 static int __genpd_runtime_resume(struct device *dev)
513 int (*cb)(struct device *__dev);
515 if (dev->type && dev->type->pm)
516 cb = dev->type->pm->runtime_resume;
517 else if (dev->class && dev->class->pm)
518 cb = dev->class->pm->runtime_resume;
519 else if (dev->bus && dev->bus->pm)
520 cb = dev->bus->pm->runtime_resume;
524 if (!cb && dev->driver && dev->driver->pm)
525 cb = dev->driver->pm->runtime_resume;
527 return cb ? cb(dev) : 0;
531 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
532 * @dev: Device to suspend.
534 * Carry out a runtime suspend of a device under the assumption that its
535 * pm_domain field points to the domain member of an object of type
536 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
538 static int genpd_runtime_suspend(struct device *dev)
540 struct generic_pm_domain *genpd;
541 bool (*suspend_ok)(struct device *__dev);
542 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
543 bool runtime_pm = pm_runtime_enabled(dev);
548 dev_dbg(dev, "%s()\n", __func__);
550 genpd = dev_to_genpd(dev);
555 * A runtime PM centric subsystem/driver may re-use the runtime PM
556 * callbacks for other purposes than runtime PM. In those scenarios
557 * runtime PM is disabled. Under these circumstances, we shall skip
558 * validating/measuring the PM QoS latency.
560 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
561 if (runtime_pm && suspend_ok && !suspend_ok(dev))
564 /* Measure suspend latency. */
567 time_start = ktime_get();
569 ret = __genpd_runtime_suspend(dev);
573 ret = genpd_stop_dev(genpd, dev);
575 __genpd_runtime_resume(dev);
579 /* Update suspend latency value if the measured time exceeds it. */
581 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
582 if (elapsed_ns > td->suspend_latency_ns) {
583 td->suspend_latency_ns = elapsed_ns;
584 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
586 genpd->max_off_time_changed = true;
587 td->constraint_changed = true;
592 * If power.irq_safe is set, this routine may be run with
593 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
595 if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
599 genpd_power_off(genpd, true, 0);
606 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
607 * @dev: Device to resume.
609 * Carry out a runtime resume of a device under the assumption that its
610 * pm_domain field points to the domain member of an object of type
611 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
613 static int genpd_runtime_resume(struct device *dev)
615 struct generic_pm_domain *genpd;
616 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
617 bool runtime_pm = pm_runtime_enabled(dev);
623 dev_dbg(dev, "%s()\n", __func__);
625 genpd = dev_to_genpd(dev);
630 * As we don't power off a non IRQ safe domain, which holds
631 * an IRQ safe device, we don't need to restore power to it.
633 if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
639 ret = genpd_power_on(genpd, 0);
646 /* Measure resume latency. */
648 if (timed && runtime_pm)
649 time_start = ktime_get();
651 ret = genpd_start_dev(genpd, dev);
655 ret = __genpd_runtime_resume(dev);
659 /* Update resume latency value if the measured time exceeds it. */
660 if (timed && runtime_pm) {
661 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
662 if (elapsed_ns > td->resume_latency_ns) {
663 td->resume_latency_ns = elapsed_ns;
664 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
666 genpd->max_off_time_changed = true;
667 td->constraint_changed = true;
674 genpd_stop_dev(genpd, dev);
676 if (!pm_runtime_is_irq_safe(dev) ||
677 (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
679 genpd_power_off(genpd, true, 0);
686 static bool pd_ignore_unused;
687 static int __init pd_ignore_unused_setup(char *__unused)
689 pd_ignore_unused = true;
692 __setup("pd_ignore_unused", pd_ignore_unused_setup);
695 * genpd_power_off_unused - Power off all PM domains with no devices in use.
697 static int __init genpd_power_off_unused(void)
699 struct generic_pm_domain *genpd;
701 if (pd_ignore_unused) {
702 pr_warn("genpd: Not disabling unused power domains\n");
706 mutex_lock(&gpd_list_lock);
708 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
709 genpd_queue_power_off_work(genpd);
711 mutex_unlock(&gpd_list_lock);
715 late_initcall(genpd_power_off_unused);
717 #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
720 * pm_genpd_present - Check if the given PM domain has been initialized.
721 * @genpd: PM domain to check.
723 static bool pm_genpd_present(const struct generic_pm_domain *genpd)
725 const struct generic_pm_domain *gpd;
727 if (IS_ERR_OR_NULL(genpd))
730 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
739 #ifdef CONFIG_PM_SLEEP
741 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
744 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
748 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
749 * @genpd: PM domain to power off, if possible.
750 * @use_lock: use the lock.
751 * @depth: nesting count for lockdep.
753 * Check if the given PM domain can be powered off (during system suspend or
754 * hibernation) and do that if so. Also, in that case propagate to its masters.
756 * This function is only called in "noirq" and "syscore" stages of system power
757 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
758 * these cases the lock must be held.
760 static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
763 struct gpd_link *link;
765 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
768 if (genpd->suspended_count != genpd->device_count
769 || atomic_read(&genpd->sd_count) > 0)
772 /* Choose the deepest state when suspending */
773 genpd->state_idx = genpd->state_count - 1;
774 if (_genpd_power_off(genpd, false))
777 genpd->status = GPD_STATE_POWER_OFF;
779 list_for_each_entry(link, &genpd->slave_links, slave_node) {
780 genpd_sd_counter_dec(link->master);
783 genpd_lock_nested(link->master, depth + 1);
785 genpd_sync_power_off(link->master, use_lock, depth + 1);
788 genpd_unlock(link->master);
793 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
794 * @genpd: PM domain to power on.
795 * @use_lock: use the lock.
796 * @depth: nesting count for lockdep.
798 * This function is only called in "noirq" and "syscore" stages of system power
799 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
800 * these cases the lock must be held.
802 static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
805 struct gpd_link *link;
807 if (genpd_status_on(genpd))
810 list_for_each_entry(link, &genpd->slave_links, slave_node) {
811 genpd_sd_counter_inc(link->master);
814 genpd_lock_nested(link->master, depth + 1);
816 genpd_sync_power_on(link->master, use_lock, depth + 1);
819 genpd_unlock(link->master);
822 _genpd_power_on(genpd, false);
824 genpd->status = GPD_STATE_ACTIVE;
828 * resume_needed - Check whether to resume a device before system suspend.
829 * @dev: Device to check.
830 * @genpd: PM domain the device belongs to.
832 * There are two cases in which a device that can wake up the system from sleep
833 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
834 * to wake up the system and it has to remain active for this purpose while the
835 * system is in the sleep state and (2) if the device is not enabled to wake up
836 * the system from sleep states and it generally doesn't generate wakeup signals
837 * by itself (those signals are generated on its behalf by other parts of the
838 * system). In the latter case it may be necessary to reconfigure the device's
839 * wakeup settings during system suspend, because it may have been set up to
840 * signal remote wakeup from the system's working state as needed by runtime PM.
841 * Return 'true' in either of the above cases.
843 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
847 if (!device_can_wakeup(dev))
850 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
851 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
855 * pm_genpd_prepare - Start power transition of a device in a PM domain.
856 * @dev: Device to start the transition of.
858 * Start a power transition of a device (during a system-wide power transition)
859 * under the assumption that its pm_domain field points to the domain member of
860 * an object of type struct generic_pm_domain representing a PM domain
861 * consisting of I/O devices.
863 static int pm_genpd_prepare(struct device *dev)
865 struct generic_pm_domain *genpd;
868 dev_dbg(dev, "%s()\n", __func__);
870 genpd = dev_to_genpd(dev);
875 * If a wakeup request is pending for the device, it should be woken up
876 * at this point and a system wakeup event should be reported if it's
877 * set up to wake up the system from sleep states.
879 if (resume_needed(dev, genpd))
880 pm_runtime_resume(dev);
884 if (genpd->prepared_count++ == 0)
885 genpd->suspended_count = 0;
889 ret = pm_generic_prepare(dev);
893 genpd->prepared_count--;
902 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
903 * @dev: Device to suspend.
905 * Stop the device and remove power from the domain if all devices in it have
908 static int pm_genpd_suspend_noirq(struct device *dev)
910 struct generic_pm_domain *genpd;
913 dev_dbg(dev, "%s()\n", __func__);
915 genpd = dev_to_genpd(dev);
919 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
922 if (genpd->dev_ops.stop && genpd->dev_ops.start) {
923 ret = pm_runtime_force_suspend(dev);
929 genpd->suspended_count++;
930 genpd_sync_power_off(genpd, true, 0);
937 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
938 * @dev: Device to resume.
940 * Restore power to the device's PM domain, if necessary, and start the device.
942 static int pm_genpd_resume_noirq(struct device *dev)
944 struct generic_pm_domain *genpd;
947 dev_dbg(dev, "%s()\n", __func__);
949 genpd = dev_to_genpd(dev);
953 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
957 genpd_sync_power_on(genpd, true, 0);
958 genpd->suspended_count--;
961 if (genpd->dev_ops.stop && genpd->dev_ops.start)
962 ret = pm_runtime_force_resume(dev);
968 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
969 * @dev: Device to freeze.
971 * Carry out a late freeze of a device under the assumption that its
972 * pm_domain field points to the domain member of an object of type
973 * struct generic_pm_domain representing a power domain consisting of I/O
976 static int pm_genpd_freeze_noirq(struct device *dev)
978 struct generic_pm_domain *genpd;
981 dev_dbg(dev, "%s()\n", __func__);
983 genpd = dev_to_genpd(dev);
987 if (genpd->dev_ops.stop && genpd->dev_ops.start)
988 ret = pm_runtime_force_suspend(dev);
994 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
995 * @dev: Device to thaw.
997 * Start the device, unless power has been removed from the domain already
998 * before the system transition.
1000 static int pm_genpd_thaw_noirq(struct device *dev)
1002 struct generic_pm_domain *genpd;
1005 dev_dbg(dev, "%s()\n", __func__);
1007 genpd = dev_to_genpd(dev);
1011 if (genpd->dev_ops.stop && genpd->dev_ops.start)
1012 ret = pm_runtime_force_resume(dev);
1018 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1019 * @dev: Device to resume.
1021 * Make sure the domain will be in the same power state as before the
1022 * hibernation the system is resuming from and start the device if necessary.
1024 static int pm_genpd_restore_noirq(struct device *dev)
1026 struct generic_pm_domain *genpd;
1029 dev_dbg(dev, "%s()\n", __func__);
1031 genpd = dev_to_genpd(dev);
1036 * At this point suspended_count == 0 means we are being run for the
1037 * first time for the given domain in the present cycle.
1040 if (genpd->suspended_count++ == 0)
1042 * The boot kernel might put the domain into arbitrary state,
1043 * so make it appear as powered off to genpd_sync_power_on(),
1044 * so that it tries to power it on in case it was really off.
1046 genpd->status = GPD_STATE_POWER_OFF;
1048 genpd_sync_power_on(genpd, true, 0);
1049 genpd_unlock(genpd);
1051 if (genpd->dev_ops.stop && genpd->dev_ops.start)
1052 ret = pm_runtime_force_resume(dev);
1058 * pm_genpd_complete - Complete power transition of a device in a power domain.
1059 * @dev: Device to complete the transition of.
1061 * Complete a power transition of a device (during a system-wide power
1062 * transition) under the assumption that its pm_domain field points to the
1063 * domain member of an object of type struct generic_pm_domain representing
1064 * a power domain consisting of I/O devices.
1066 static void pm_genpd_complete(struct device *dev)
1068 struct generic_pm_domain *genpd;
1070 dev_dbg(dev, "%s()\n", __func__);
1072 genpd = dev_to_genpd(dev);
1076 pm_generic_complete(dev);
1080 genpd->prepared_count--;
1081 if (!genpd->prepared_count)
1082 genpd_queue_power_off_work(genpd);
1084 genpd_unlock(genpd);
1088 * genpd_syscore_switch - Switch power during system core suspend or resume.
1089 * @dev: Device that normally is marked as "always on" to switch power for.
1091 * This routine may only be called during the system core (syscore) suspend or
1092 * resume phase for devices whose "always on" flags are set.
1094 static void genpd_syscore_switch(struct device *dev, bool suspend)
1096 struct generic_pm_domain *genpd;
1098 genpd = dev_to_genpd(dev);
1099 if (!pm_genpd_present(genpd))
1103 genpd->suspended_count++;
1104 genpd_sync_power_off(genpd, false, 0);
1106 genpd_sync_power_on(genpd, false, 0);
1107 genpd->suspended_count--;
1111 void pm_genpd_syscore_poweroff(struct device *dev)
1113 genpd_syscore_switch(dev, true);
1115 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1117 void pm_genpd_syscore_poweron(struct device *dev)
1119 genpd_syscore_switch(dev, false);
1121 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1123 #else /* !CONFIG_PM_SLEEP */
1125 #define pm_genpd_prepare NULL
1126 #define pm_genpd_suspend_noirq NULL
1127 #define pm_genpd_resume_noirq NULL
1128 #define pm_genpd_freeze_noirq NULL
1129 #define pm_genpd_thaw_noirq NULL
1130 #define pm_genpd_restore_noirq NULL
1131 #define pm_genpd_complete NULL
1133 #endif /* CONFIG_PM_SLEEP */
1135 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1136 struct generic_pm_domain *genpd,
1137 struct gpd_timing_data *td)
1139 struct generic_pm_domain_data *gpd_data;
1142 ret = dev_pm_get_subsys_data(dev);
1144 return ERR_PTR(ret);
1146 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1155 gpd_data->base.dev = dev;
1156 gpd_data->td.constraint_changed = true;
1157 gpd_data->td.effective_constraint_ns = -1;
1158 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1160 spin_lock_irq(&dev->power.lock);
1162 if (dev->power.subsys_data->domain_data) {
1167 dev->power.subsys_data->domain_data = &gpd_data->base;
1169 spin_unlock_irq(&dev->power.lock);
1171 dev_pm_domain_set(dev, &genpd->domain);
1176 spin_unlock_irq(&dev->power.lock);
1179 dev_pm_put_subsys_data(dev);
1180 return ERR_PTR(ret);
1183 static void genpd_free_dev_data(struct device *dev,
1184 struct generic_pm_domain_data *gpd_data)
1186 dev_pm_domain_set(dev, NULL);
1188 spin_lock_irq(&dev->power.lock);
1190 dev->power.subsys_data->domain_data = NULL;
1192 spin_unlock_irq(&dev->power.lock);
1195 dev_pm_put_subsys_data(dev);
1198 static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1199 struct gpd_timing_data *td)
1201 struct generic_pm_domain_data *gpd_data;
1204 dev_dbg(dev, "%s()\n", __func__);
1206 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1209 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1210 if (IS_ERR(gpd_data))
1211 return PTR_ERR(gpd_data);
1215 if (genpd->prepared_count > 0) {
1220 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1224 genpd->device_count++;
1225 genpd->max_off_time_changed = true;
1227 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1230 genpd_unlock(genpd);
1233 genpd_free_dev_data(dev, gpd_data);
1235 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1241 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1242 * @genpd: PM domain to add the device to.
1243 * @dev: Device to be added.
1244 * @td: Set of PM QoS timing parameters to attach to the device.
1246 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1247 struct gpd_timing_data *td)
1251 mutex_lock(&gpd_list_lock);
1252 ret = genpd_add_device(genpd, dev, td);
1253 mutex_unlock(&gpd_list_lock);
1257 EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
1259 static int genpd_remove_device(struct generic_pm_domain *genpd,
1262 struct generic_pm_domain_data *gpd_data;
1263 struct pm_domain_data *pdd;
1266 dev_dbg(dev, "%s()\n", __func__);
1268 pdd = dev->power.subsys_data->domain_data;
1269 gpd_data = to_gpd_data(pdd);
1270 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1274 if (genpd->prepared_count > 0) {
1279 genpd->device_count--;
1280 genpd->max_off_time_changed = true;
1282 if (genpd->detach_dev)
1283 genpd->detach_dev(genpd, dev);
1285 list_del_init(&pdd->list_node);
1287 genpd_unlock(genpd);
1289 genpd_free_dev_data(dev, gpd_data);
1294 genpd_unlock(genpd);
1295 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1301 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1302 * @genpd: PM domain to remove the device from.
1303 * @dev: Device to be removed.
1305 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1308 if (!genpd || genpd != genpd_lookup_dev(dev))
1311 return genpd_remove_device(genpd, dev);
1313 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1315 static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1316 struct generic_pm_domain *subdomain)
1318 struct gpd_link *link, *itr;
1321 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1322 || genpd == subdomain)
1326 * If the domain can be powered on/off in an IRQ safe
1327 * context, ensure that the subdomain can also be
1328 * powered on/off in that context.
1330 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1331 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1332 genpd->name, subdomain->name);
1336 link = kzalloc(sizeof(*link), GFP_KERNEL);
1340 genpd_lock(subdomain);
1341 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1343 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1348 list_for_each_entry(itr, &genpd->master_links, master_node) {
1349 if (itr->slave == subdomain && itr->master == genpd) {
1355 link->master = genpd;
1356 list_add_tail(&link->master_node, &genpd->master_links);
1357 link->slave = subdomain;
1358 list_add_tail(&link->slave_node, &subdomain->slave_links);
1359 if (genpd_status_on(subdomain))
1360 genpd_sd_counter_inc(genpd);
1363 genpd_unlock(genpd);
1364 genpd_unlock(subdomain);
1371 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1372 * @genpd: Master PM domain to add the subdomain to.
1373 * @subdomain: Subdomain to be added.
1375 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1376 struct generic_pm_domain *subdomain)
1380 mutex_lock(&gpd_list_lock);
1381 ret = genpd_add_subdomain(genpd, subdomain);
1382 mutex_unlock(&gpd_list_lock);
1386 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1389 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1390 * @genpd: Master PM domain to remove the subdomain from.
1391 * @subdomain: Subdomain to be removed.
1393 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1394 struct generic_pm_domain *subdomain)
1396 struct gpd_link *link;
1399 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1402 genpd_lock(subdomain);
1403 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1405 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1406 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1412 list_for_each_entry(link, &genpd->master_links, master_node) {
1413 if (link->slave != subdomain)
1416 list_del(&link->master_node);
1417 list_del(&link->slave_node);
1419 if (genpd_status_on(subdomain))
1420 genpd_sd_counter_dec(genpd);
1427 genpd_unlock(genpd);
1428 genpd_unlock(subdomain);
1432 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1434 static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1436 struct genpd_power_state *state;
1438 state = kzalloc(sizeof(*state), GFP_KERNEL);
1442 genpd->states = state;
1443 genpd->state_count = 1;
1444 genpd->free = state;
1449 static void genpd_lock_init(struct generic_pm_domain *genpd)
1451 if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1452 spin_lock_init(&genpd->slock);
1453 genpd->lock_ops = &genpd_spin_ops;
1455 mutex_init(&genpd->mlock);
1456 genpd->lock_ops = &genpd_mtx_ops;
1461 * pm_genpd_init - Initialize a generic I/O PM domain object.
1462 * @genpd: PM domain object to initialize.
1463 * @gov: PM domain governor to associate with the domain (may be NULL).
1464 * @is_off: Initial value of the domain's power_is_off field.
1466 * Returns 0 on successful initialization, else a negative error code.
1468 int pm_genpd_init(struct generic_pm_domain *genpd,
1469 struct dev_power_governor *gov, bool is_off)
1473 if (IS_ERR_OR_NULL(genpd))
1476 INIT_LIST_HEAD(&genpd->master_links);
1477 INIT_LIST_HEAD(&genpd->slave_links);
1478 INIT_LIST_HEAD(&genpd->dev_list);
1479 genpd_lock_init(genpd);
1481 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1482 atomic_set(&genpd->sd_count, 0);
1483 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1484 genpd->device_count = 0;
1485 genpd->max_off_time_ns = -1;
1486 genpd->max_off_time_changed = true;
1487 genpd->provider = NULL;
1488 genpd->has_provider = false;
1489 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1490 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
1491 genpd->domain.ops.prepare = pm_genpd_prepare;
1492 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1493 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1494 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1495 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1496 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1497 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1498 genpd->domain.ops.complete = pm_genpd_complete;
1500 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1501 genpd->dev_ops.stop = pm_clk_suspend;
1502 genpd->dev_ops.start = pm_clk_resume;
1505 /* Always-on domains must be powered on at initialization. */
1506 if (genpd_is_always_on(genpd) && !genpd_status_on(genpd))
1509 /* Use only one "off" state if there were no states declared */
1510 if (genpd->state_count == 0) {
1511 ret = genpd_set_default_power_state(genpd);
1516 mutex_lock(&gpd_list_lock);
1517 list_add(&genpd->gpd_list_node, &gpd_list);
1518 mutex_unlock(&gpd_list_lock);
1522 EXPORT_SYMBOL_GPL(pm_genpd_init);
1524 static int genpd_remove(struct generic_pm_domain *genpd)
1526 struct gpd_link *l, *link;
1528 if (IS_ERR_OR_NULL(genpd))
1533 if (genpd->has_provider) {
1534 genpd_unlock(genpd);
1535 pr_err("Provider present, unable to remove %s\n", genpd->name);
1539 if (!list_empty(&genpd->master_links) || genpd->device_count) {
1540 genpd_unlock(genpd);
1541 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1545 list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
1546 list_del(&link->master_node);
1547 list_del(&link->slave_node);
1551 list_del(&genpd->gpd_list_node);
1552 genpd_unlock(genpd);
1553 cancel_work_sync(&genpd->power_off_work);
1555 pr_debug("%s: removed %s\n", __func__, genpd->name);
1561 * pm_genpd_remove - Remove a generic I/O PM domain
1562 * @genpd: Pointer to PM domain that is to be removed.
1564 * To remove the PM domain, this function:
1565 * - Removes the PM domain as a subdomain to any parent domains,
1567 * - Removes the PM domain from the list of registered PM domains.
1569 * The PM domain will only be removed, if the associated provider has
1570 * been removed, it is not a parent to any other PM domain and has no
1571 * devices associated with it.
1573 int pm_genpd_remove(struct generic_pm_domain *genpd)
1577 mutex_lock(&gpd_list_lock);
1578 ret = genpd_remove(genpd);
1579 mutex_unlock(&gpd_list_lock);
1583 EXPORT_SYMBOL_GPL(pm_genpd_remove);
1585 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1587 typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
1591 * Device Tree based PM domain providers.
1593 * The code below implements generic device tree based PM domain providers that
1594 * bind device tree nodes with generic PM domains registered in the system.
1596 * Any driver that registers generic PM domains and needs to support binding of
1597 * devices to these domains is supposed to register a PM domain provider, which
1598 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1600 * Two simple mapping functions have been provided for convenience:
1601 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1602 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1607 * struct of_genpd_provider - PM domain provider registration structure
1608 * @link: Entry in global list of PM domain providers
1609 * @node: Pointer to device tree node of PM domain provider
1610 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1612 * @data: context pointer to be passed into @xlate callback
1614 struct of_genpd_provider {
1615 struct list_head link;
1616 struct device_node *node;
1617 genpd_xlate_t xlate;
1621 /* List of registered PM domain providers. */
1622 static LIST_HEAD(of_genpd_providers);
1623 /* Mutex to protect the list above. */
1624 static DEFINE_MUTEX(of_genpd_mutex);
1627 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1628 * @genpdspec: OF phandle args to map into a PM domain
1629 * @data: xlate function private data - pointer to struct generic_pm_domain
1631 * This is a generic xlate function that can be used to model PM domains that
1632 * have their own device tree nodes. The private data of xlate function needs
1633 * to be a valid pointer to struct generic_pm_domain.
1635 static struct generic_pm_domain *genpd_xlate_simple(
1636 struct of_phandle_args *genpdspec,
1639 if (genpdspec->args_count != 0)
1640 return ERR_PTR(-EINVAL);
1645 * genpd_xlate_onecell() - Xlate function using a single index.
1646 * @genpdspec: OF phandle args to map into a PM domain
1647 * @data: xlate function private data - pointer to struct genpd_onecell_data
1649 * This is a generic xlate function that can be used to model simple PM domain
1650 * controllers that have one device tree node and provide multiple PM domains.
1651 * A single cell is used as an index into an array of PM domains specified in
1652 * the genpd_onecell_data struct when registering the provider.
1654 static struct generic_pm_domain *genpd_xlate_onecell(
1655 struct of_phandle_args *genpdspec,
1658 struct genpd_onecell_data *genpd_data = data;
1659 unsigned int idx = genpdspec->args[0];
1661 if (genpdspec->args_count != 1)
1662 return ERR_PTR(-EINVAL);
1664 if (idx >= genpd_data->num_domains) {
1665 pr_err("%s: invalid domain index %u\n", __func__, idx);
1666 return ERR_PTR(-EINVAL);
1669 if (!genpd_data->domains[idx])
1670 return ERR_PTR(-ENOENT);
1672 return genpd_data->domains[idx];
1676 * genpd_add_provider() - Register a PM domain provider for a node
1677 * @np: Device node pointer associated with the PM domain provider.
1678 * @xlate: Callback for decoding PM domain from phandle arguments.
1679 * @data: Context pointer for @xlate callback.
1681 static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1684 struct of_genpd_provider *cp;
1686 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1690 cp->node = of_node_get(np);
1694 mutex_lock(&of_genpd_mutex);
1695 list_add(&cp->link, &of_genpd_providers);
1696 mutex_unlock(&of_genpd_mutex);
1697 pr_debug("Added domain provider from %s\n", np->full_name);
1703 * of_genpd_add_provider_simple() - Register a simple PM domain provider
1704 * @np: Device node pointer associated with the PM domain provider.
1705 * @genpd: Pointer to PM domain associated with the PM domain provider.
1707 int of_genpd_add_provider_simple(struct device_node *np,
1708 struct generic_pm_domain *genpd)
1715 mutex_lock(&gpd_list_lock);
1717 if (pm_genpd_present(genpd)) {
1718 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
1720 genpd->provider = &np->fwnode;
1721 genpd->has_provider = true;
1725 mutex_unlock(&gpd_list_lock);
1729 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
1732 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
1733 * @np: Device node pointer associated with the PM domain provider.
1734 * @data: Pointer to the data associated with the PM domain provider.
1736 int of_genpd_add_provider_onecell(struct device_node *np,
1737 struct genpd_onecell_data *data)
1745 mutex_lock(&gpd_list_lock);
1747 for (i = 0; i < data->num_domains; i++) {
1748 if (!data->domains[i])
1750 if (!pm_genpd_present(data->domains[i]))
1753 data->domains[i]->provider = &np->fwnode;
1754 data->domains[i]->has_provider = true;
1757 ret = genpd_add_provider(np, genpd_xlate_onecell, data);
1761 mutex_unlock(&gpd_list_lock);
1767 if (!data->domains[i])
1769 data->domains[i]->provider = NULL;
1770 data->domains[i]->has_provider = false;
1773 mutex_unlock(&gpd_list_lock);
1777 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
1780 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1781 * @np: Device node pointer associated with the PM domain provider
1783 void of_genpd_del_provider(struct device_node *np)
1785 struct of_genpd_provider *cp;
1786 struct generic_pm_domain *gpd;
1788 mutex_lock(&gpd_list_lock);
1789 mutex_lock(&of_genpd_mutex);
1790 list_for_each_entry(cp, &of_genpd_providers, link) {
1791 if (cp->node == np) {
1793 * For each PM domain associated with the
1794 * provider, set the 'has_provider' to false
1795 * so that the PM domain can be safely removed.
1797 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
1798 if (gpd->provider == &np->fwnode)
1799 gpd->has_provider = false;
1801 list_del(&cp->link);
1802 of_node_put(cp->node);
1807 mutex_unlock(&of_genpd_mutex);
1808 mutex_unlock(&gpd_list_lock);
1810 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1813 * genpd_get_from_provider() - Look-up PM domain
1814 * @genpdspec: OF phandle args to use for look-up
1816 * Looks for a PM domain provider under the node specified by @genpdspec and if
1817 * found, uses xlate function of the provider to map phandle args to a PM
1820 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1823 static struct generic_pm_domain *genpd_get_from_provider(
1824 struct of_phandle_args *genpdspec)
1826 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1827 struct of_genpd_provider *provider;
1830 return ERR_PTR(-EINVAL);
1832 mutex_lock(&of_genpd_mutex);
1834 /* Check if we have such a provider in our array */
1835 list_for_each_entry(provider, &of_genpd_providers, link) {
1836 if (provider->node == genpdspec->np)
1837 genpd = provider->xlate(genpdspec, provider->data);
1842 mutex_unlock(&of_genpd_mutex);
1848 * of_genpd_add_device() - Add a device to an I/O PM domain
1849 * @genpdspec: OF phandle args to use for look-up PM domain
1850 * @dev: Device to be added.
1852 * Looks-up an I/O PM domain based upon phandle args provided and adds
1853 * the device to the PM domain. Returns a negative error code on failure.
1855 int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
1857 struct generic_pm_domain *genpd;
1860 mutex_lock(&gpd_list_lock);
1862 genpd = genpd_get_from_provider(genpdspec);
1863 if (IS_ERR(genpd)) {
1864 ret = PTR_ERR(genpd);
1868 ret = genpd_add_device(genpd, dev, NULL);
1871 mutex_unlock(&gpd_list_lock);
1875 EXPORT_SYMBOL_GPL(of_genpd_add_device);
1878 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1879 * @parent_spec: OF phandle args to use for parent PM domain look-up
1880 * @subdomain_spec: OF phandle args to use for subdomain look-up
1882 * Looks-up a parent PM domain and subdomain based upon phandle args
1883 * provided and adds the subdomain to the parent PM domain. Returns a
1884 * negative error code on failure.
1886 int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
1887 struct of_phandle_args *subdomain_spec)
1889 struct generic_pm_domain *parent, *subdomain;
1892 mutex_lock(&gpd_list_lock);
1894 parent = genpd_get_from_provider(parent_spec);
1895 if (IS_ERR(parent)) {
1896 ret = PTR_ERR(parent);
1900 subdomain = genpd_get_from_provider(subdomain_spec);
1901 if (IS_ERR(subdomain)) {
1902 ret = PTR_ERR(subdomain);
1906 ret = genpd_add_subdomain(parent, subdomain);
1909 mutex_unlock(&gpd_list_lock);
1913 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
1916 * of_genpd_remove_last - Remove the last PM domain registered for a provider
1917 * @provider: Pointer to device structure associated with provider
1919 * Find the last PM domain that was added by a particular provider and
1920 * remove this PM domain from the list of PM domains. The provider is
1921 * identified by the 'provider' device structure that is passed. The PM
1922 * domain will only be removed, if the provider associated with domain
1925 * Returns a valid pointer to struct generic_pm_domain on success or
1926 * ERR_PTR() on failure.
1928 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
1930 struct generic_pm_domain *gpd, *genpd = ERR_PTR(-ENOENT);
1933 if (IS_ERR_OR_NULL(np))
1934 return ERR_PTR(-EINVAL);
1936 mutex_lock(&gpd_list_lock);
1937 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1938 if (gpd->provider == &np->fwnode) {
1939 ret = genpd_remove(gpd);
1940 genpd = ret ? ERR_PTR(ret) : gpd;
1944 mutex_unlock(&gpd_list_lock);
1948 EXPORT_SYMBOL_GPL(of_genpd_remove_last);
1951 * genpd_dev_pm_detach - Detach a device from its PM domain.
1952 * @dev: Device to detach.
1953 * @power_off: Currently not used
1955 * Try to locate a corresponding generic PM domain, which the device was
1956 * attached to previously. If such is found, the device is detached from it.
1958 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1960 struct generic_pm_domain *pd;
1964 pd = dev_to_genpd(dev);
1968 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1970 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1971 ret = genpd_remove_device(pd, dev);
1980 dev_err(dev, "failed to remove from PM domain %s: %d",
1985 /* Check if PM domain can be powered off after removing this device. */
1986 genpd_queue_power_off_work(pd);
1989 static void genpd_dev_pm_sync(struct device *dev)
1991 struct generic_pm_domain *pd;
1993 pd = dev_to_genpd(dev);
1997 genpd_queue_power_off_work(pd);
2001 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2002 * @dev: Device to attach.
2004 * Parse device's OF node to find a PM domain specifier. If such is found,
2005 * attaches the device to retrieved pm_domain ops.
2007 * Both generic and legacy Samsung-specific DT bindings are supported to keep
2008 * backwards compatibility with existing DTBs.
2010 * Returns 0 on successfully attached PM domain or negative error code. Note
2011 * that if a power-domain exists for the device, but it cannot be found or
2012 * turned on, then return -EPROBE_DEFER to ensure that the device is not
2013 * probed and to re-try again later.
2015 int genpd_dev_pm_attach(struct device *dev)
2017 struct of_phandle_args pd_args;
2018 struct generic_pm_domain *pd;
2028 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2029 "#power-domain-cells", 0, &pd_args);
2035 * Try legacy Samsung-specific bindings
2036 * (for backwards compatibility of DT ABI)
2038 pd_args.args_count = 0;
2039 pd_args.np = of_parse_phandle(dev->of_node,
2040 "samsung,power-domain", 0);
2045 mutex_lock(&gpd_list_lock);
2046 pd = genpd_get_from_provider(&pd_args);
2047 of_node_put(pd_args.np);
2049 mutex_unlock(&gpd_list_lock);
2050 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2051 __func__, PTR_ERR(pd));
2052 return -EPROBE_DEFER;
2055 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2057 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2058 ret = genpd_add_device(pd, dev, NULL);
2065 mutex_unlock(&gpd_list_lock);
2068 if (ret != -EPROBE_DEFER)
2069 dev_err(dev, "failed to add to PM domain %s: %d",
2074 dev->pm_domain->detach = genpd_dev_pm_detach;
2075 dev->pm_domain->sync = genpd_dev_pm_sync;
2078 ret = genpd_power_on(pd, 0);
2081 return ret ? -EPROBE_DEFER : 0;
2083 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2085 static const struct of_device_id idle_state_match[] = {
2086 { .compatible = "domain-idle-state", },
2090 static int genpd_parse_state(struct genpd_power_state *genpd_state,
2091 struct device_node *state_node)
2095 u32 entry_latency, exit_latency;
2097 err = of_property_read_u32(state_node, "entry-latency-us",
2100 pr_debug(" * %s missing entry-latency-us property\n",
2101 state_node->full_name);
2105 err = of_property_read_u32(state_node, "exit-latency-us",
2108 pr_debug(" * %s missing exit-latency-us property\n",
2109 state_node->full_name);
2113 err = of_property_read_u32(state_node, "min-residency-us", &residency);
2115 genpd_state->residency_ns = 1000 * residency;
2117 genpd_state->power_on_latency_ns = 1000 * exit_latency;
2118 genpd_state->power_off_latency_ns = 1000 * entry_latency;
2119 genpd_state->fwnode = &state_node->fwnode;
2125 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2127 * @dn: The genpd device node
2128 * @states: The pointer to which the state array will be saved.
2129 * @n: The count of elements in the array returned from this function.
2131 * Returns the device states parsed from the OF node. The memory for the states
2132 * is allocated by this function and is the responsibility of the caller to
2133 * free the memory after use.
2135 int of_genpd_parse_idle_states(struct device_node *dn,
2136 struct genpd_power_state **states, int *n)
2138 struct genpd_power_state *st;
2139 struct device_node *np;
2143 struct of_phandle_iterator it;
2144 const struct of_device_id *match_id;
2146 count = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2150 st = kcalloc(count, sizeof(*st), GFP_KERNEL);
2154 /* Loop over the phandles until all the requested entry is found */
2155 of_for_each_phandle(&it, err, dn, "domain-idle-states", NULL, 0) {
2157 match_id = of_match_node(idle_state_match, np);
2160 ret = genpd_parse_state(&st[i++], np);
2163 ("Parsing idle state node %s failed with err %d\n",
2164 np->full_name, ret);
2179 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2181 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2184 /*** debugfs support ***/
2186 #ifdef CONFIG_DEBUG_FS
2187 #include <linux/pm.h>
2188 #include <linux/device.h>
2189 #include <linux/debugfs.h>
2190 #include <linux/seq_file.h>
2191 #include <linux/init.h>
2192 #include <linux/kobject.h>
2193 static struct dentry *pm_genpd_debugfs_dir;
2196 * TODO: This function is a slightly modified version of rtpm_status_show
2197 * from sysfs.c, so generalize it.
2199 static void rtpm_status_str(struct seq_file *s, struct device *dev)
2201 static const char * const status_lookup[] = {
2202 [RPM_ACTIVE] = "active",
2203 [RPM_RESUMING] = "resuming",
2204 [RPM_SUSPENDED] = "suspended",
2205 [RPM_SUSPENDING] = "suspending"
2209 if (dev->power.runtime_error)
2211 else if (dev->power.disable_depth)
2213 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2214 p = status_lookup[dev->power.runtime_status];
2221 static int pm_genpd_summary_one(struct seq_file *s,
2222 struct generic_pm_domain *genpd)
2224 static const char * const status_lookup[] = {
2225 [GPD_STATE_ACTIVE] = "on",
2226 [GPD_STATE_POWER_OFF] = "off"
2228 struct pm_domain_data *pm_data;
2229 const char *kobj_path;
2230 struct gpd_link *link;
2234 ret = genpd_lock_interruptible(genpd);
2236 return -ERESTARTSYS;
2238 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2240 if (!genpd_status_on(genpd))
2241 snprintf(state, sizeof(state), "%s-%u",
2242 status_lookup[genpd->status], genpd->state_idx);
2244 snprintf(state, sizeof(state), "%s",
2245 status_lookup[genpd->status]);
2246 seq_printf(s, "%-30s %-15s ", genpd->name, state);
2249 * Modifications on the list require holding locks on both
2250 * master and slave, so we are safe.
2251 * Also genpd->name is immutable.
2253 list_for_each_entry(link, &genpd->master_links, master_node) {
2254 seq_printf(s, "%s", link->slave->name);
2255 if (!list_is_last(&link->master_node, &genpd->master_links))
2259 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2260 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2261 genpd_is_irq_safe(genpd) ?
2262 GFP_ATOMIC : GFP_KERNEL);
2263 if (kobj_path == NULL)
2266 seq_printf(s, "\n %-50s ", kobj_path);
2267 rtpm_status_str(s, pm_data->dev);
2273 genpd_unlock(genpd);
2278 static int pm_genpd_summary_show(struct seq_file *s, void *data)
2280 struct generic_pm_domain *genpd;
2283 seq_puts(s, "domain status slaves\n");
2284 seq_puts(s, " /device runtime status\n");
2285 seq_puts(s, "----------------------------------------------------------------------\n");
2287 ret = mutex_lock_interruptible(&gpd_list_lock);
2289 return -ERESTARTSYS;
2291 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2292 ret = pm_genpd_summary_one(s, genpd);
2296 mutex_unlock(&gpd_list_lock);
2301 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
2303 return single_open(file, pm_genpd_summary_show, NULL);
2306 static const struct file_operations pm_genpd_summary_fops = {
2307 .open = pm_genpd_summary_open,
2309 .llseek = seq_lseek,
2310 .release = single_release,
2313 static int __init pm_genpd_debug_init(void)
2317 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2319 if (!pm_genpd_debugfs_dir)
2322 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
2323 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
2329 late_initcall(pm_genpd_debug_init);
2331 static void __exit pm_genpd_debug_exit(void)
2333 debugfs_remove_recursive(pm_genpd_debugfs_dir);
2335 __exitcall(pm_genpd_debug_exit);
2336 #endif /* CONFIG_DEBUG_FS */