1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/power/domain.c - Common code related to device power domains.
7 #define pr_fmt(fmt) "PM: " fmt
9 #include <linux/delay.h>
10 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_opp.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/pm_domain.h>
16 #include <linux/pm_qos.h>
17 #include <linux/pm_clock.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/sched.h>
21 #include <linux/suspend.h>
22 #include <linux/export.h>
23 #include <linux/cpu.h>
27 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
29 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
31 type (*__routine)(struct device *__d); \
32 type __ret = (type)0; \
34 __routine = genpd->dev_ops.callback; \
36 __ret = __routine(dev); \
41 static LIST_HEAD(gpd_list);
42 static DEFINE_MUTEX(gpd_list_lock);
44 struct genpd_lock_ops {
45 void (*lock)(struct generic_pm_domain *genpd);
46 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
47 int (*lock_interruptible)(struct generic_pm_domain *genpd);
48 void (*unlock)(struct generic_pm_domain *genpd);
51 static void genpd_lock_mtx(struct generic_pm_domain *genpd)
53 mutex_lock(&genpd->mlock);
56 static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
59 mutex_lock_nested(&genpd->mlock, depth);
62 static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
64 return mutex_lock_interruptible(&genpd->mlock);
67 static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
69 return mutex_unlock(&genpd->mlock);
72 static const struct genpd_lock_ops genpd_mtx_ops = {
73 .lock = genpd_lock_mtx,
74 .lock_nested = genpd_lock_nested_mtx,
75 .lock_interruptible = genpd_lock_interruptible_mtx,
76 .unlock = genpd_unlock_mtx,
79 static void genpd_lock_spin(struct generic_pm_domain *genpd)
80 __acquires(&genpd->slock)
84 spin_lock_irqsave(&genpd->slock, flags);
85 genpd->lock_flags = flags;
88 static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
90 __acquires(&genpd->slock)
94 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
95 genpd->lock_flags = flags;
98 static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
99 __acquires(&genpd->slock)
103 spin_lock_irqsave(&genpd->slock, flags);
104 genpd->lock_flags = flags;
108 static void genpd_unlock_spin(struct generic_pm_domain *genpd)
109 __releases(&genpd->slock)
111 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
114 static const struct genpd_lock_ops genpd_spin_ops = {
115 .lock = genpd_lock_spin,
116 .lock_nested = genpd_lock_nested_spin,
117 .lock_interruptible = genpd_lock_interruptible_spin,
118 .unlock = genpd_unlock_spin,
121 #define genpd_lock(p) p->lock_ops->lock(p)
122 #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
123 #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
124 #define genpd_unlock(p) p->lock_ops->unlock(p)
126 #define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
127 #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
128 #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
129 #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
130 #define genpd_is_cpu_domain(genpd) (genpd->flags & GENPD_FLAG_CPU_DOMAIN)
131 #define genpd_is_rpm_always_on(genpd) (genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
133 static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
134 const struct generic_pm_domain *genpd)
138 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
141 * Warn once if an IRQ safe device is attached to a no sleep domain, as
142 * to indicate a suboptimal configuration for PM. For an always on
143 * domain this isn't case, thus don't warn.
145 if (ret && !genpd_is_always_on(genpd))
146 dev_warn_once(dev, "PM domain %s will not be powered off\n",
153 * Get the generic PM domain for a particular struct device.
154 * This validates the struct device pointer, the PM domain pointer,
155 * and checks that the PM domain pointer is a real generic PM domain.
156 * Any failure results in NULL being returned.
158 static struct generic_pm_domain *genpd_lookup_dev(struct device *dev)
160 struct generic_pm_domain *genpd = NULL, *gpd;
162 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
165 mutex_lock(&gpd_list_lock);
166 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
167 if (&gpd->domain == dev->pm_domain) {
172 mutex_unlock(&gpd_list_lock);
178 * This should only be used where we are certain that the pm_domain
179 * attached to the device is a genpd domain.
181 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
183 if (IS_ERR_OR_NULL(dev->pm_domain))
184 return ERR_PTR(-EINVAL);
186 return pd_to_genpd(dev->pm_domain);
189 static int genpd_stop_dev(const struct generic_pm_domain *genpd,
192 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
195 static int genpd_start_dev(const struct generic_pm_domain *genpd,
198 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
201 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
205 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
206 ret = !!atomic_dec_and_test(&genpd->sd_count);
211 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
213 atomic_inc(&genpd->sd_count);
214 smp_mb__after_atomic();
217 #ifdef CONFIG_DEBUG_FS
218 static void genpd_update_accounting(struct generic_pm_domain *genpd)
223 delta = ktime_sub(now, genpd->accounting_time);
226 * If genpd->status is active, it means we are just
227 * out of off and so update the idle time and vice
230 if (genpd->status == GPD_STATE_ACTIVE) {
231 int state_idx = genpd->state_idx;
233 genpd->states[state_idx].idle_time =
234 ktime_add(genpd->states[state_idx].idle_time, delta);
236 genpd->on_time = ktime_add(genpd->on_time, delta);
239 genpd->accounting_time = now;
242 static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
245 static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd,
248 struct generic_pm_domain_data *pd_data;
249 struct pm_domain_data *pdd;
250 struct gpd_link *link;
252 /* New requested state is same as Max requested state */
253 if (state == genpd->performance_state)
256 /* New requested state is higher than Max requested state */
257 if (state > genpd->performance_state)
260 /* Traverse all devices within the domain */
261 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
262 pd_data = to_gpd_data(pdd);
264 if (pd_data->performance_state > state)
265 state = pd_data->performance_state;
269 * Traverse all sub-domains within the domain. This can be
270 * done without any additional locking as the link->performance_state
271 * field is protected by the master genpd->lock, which is already taken.
273 * Also note that link->performance_state (subdomain's performance state
274 * requirement to master domain) is different from
275 * link->slave->performance_state (current performance state requirement
276 * of the devices/sub-domains of the subdomain) and so can have a
279 * Note that we also take vote from powered-off sub-domains into account
280 * as the same is done for devices right now.
282 list_for_each_entry(link, &genpd->master_links, master_node) {
283 if (link->performance_state > state)
284 state = link->performance_state;
290 static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
291 unsigned int state, int depth)
293 struct generic_pm_domain *master;
294 struct gpd_link *link;
295 int master_state, ret;
297 if (state == genpd->performance_state)
300 /* Propagate to masters of genpd */
301 list_for_each_entry(link, &genpd->slave_links, slave_node) {
302 master = link->master;
304 if (!master->set_performance_state)
307 /* Find master's performance state */
308 ret = dev_pm_opp_xlate_performance_state(genpd->opp_table,
311 if (unlikely(ret < 0))
316 genpd_lock_nested(master, depth + 1);
318 link->prev_performance_state = link->performance_state;
319 link->performance_state = master_state;
320 master_state = _genpd_reeval_performance_state(master,
322 ret = _genpd_set_performance_state(master, master_state, depth + 1);
324 link->performance_state = link->prev_performance_state;
326 genpd_unlock(master);
332 ret = genpd->set_performance_state(genpd, state);
336 genpd->performance_state = state;
340 /* Encountered an error, lets rollback */
341 list_for_each_entry_continue_reverse(link, &genpd->slave_links,
343 master = link->master;
345 if (!master->set_performance_state)
348 genpd_lock_nested(master, depth + 1);
350 master_state = link->prev_performance_state;
351 link->performance_state = master_state;
353 master_state = _genpd_reeval_performance_state(master,
355 if (_genpd_set_performance_state(master, master_state, depth + 1)) {
356 pr_err("%s: Failed to roll back to %d performance state\n",
357 master->name, master_state);
360 genpd_unlock(master);
367 * dev_pm_genpd_set_performance_state- Set performance state of device's power
370 * @dev: Device for which the performance-state needs to be set.
371 * @state: Target performance state of the device. This can be set as 0 when the
372 * device doesn't have any performance state constraints left (And so
373 * the device wouldn't participate anymore to find the target
374 * performance state of the genpd).
376 * It is assumed that the users guarantee that the genpd wouldn't be detached
377 * while this routine is getting called.
379 * Returns 0 on success and negative error values on failures.
381 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
383 struct generic_pm_domain *genpd;
384 struct generic_pm_domain_data *gpd_data;
388 genpd = dev_to_genpd(dev);
392 if (unlikely(!genpd->set_performance_state))
395 if (WARN_ON(!dev->power.subsys_data ||
396 !dev->power.subsys_data->domain_data))
401 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
402 prev = gpd_data->performance_state;
403 gpd_data->performance_state = state;
405 state = _genpd_reeval_performance_state(genpd, state);
406 ret = _genpd_set_performance_state(genpd, state, 0);
408 gpd_data->performance_state = prev;
414 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
416 static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
418 unsigned int state_idx = genpd->state_idx;
423 if (!genpd->power_on)
427 return genpd->power_on(genpd);
429 time_start = ktime_get();
430 ret = genpd->power_on(genpd);
434 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
435 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
438 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
439 genpd->max_off_time_changed = true;
440 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
441 genpd->name, "on", elapsed_ns);
446 static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
448 unsigned int state_idx = genpd->state_idx;
453 if (!genpd->power_off)
457 return genpd->power_off(genpd);
459 time_start = ktime_get();
460 ret = genpd->power_off(genpd);
464 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
465 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
468 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
469 genpd->max_off_time_changed = true;
470 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
471 genpd->name, "off", elapsed_ns);
477 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
478 * @genpd: PM domain to power off.
480 * Queue up the execution of genpd_power_off() unless it's already been done
483 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
485 queue_work(pm_wq, &genpd->power_off_work);
489 * genpd_power_off - Remove power from a given PM domain.
490 * @genpd: PM domain to power down.
491 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
492 * RPM status of the releated device is in an intermediate state, not yet turned
493 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
494 * be RPM_SUSPENDED, while it tries to power off the PM domain.
496 * If all of the @genpd's devices have been suspended and all of its subdomains
497 * have been powered down, remove power from @genpd.
499 static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
502 struct pm_domain_data *pdd;
503 struct gpd_link *link;
504 unsigned int not_suspended = 0;
507 * Do not try to power off the domain in the following situations:
508 * (1) The domain is already in the "power off" state.
509 * (2) System suspend is in progress.
511 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
515 * Abort power off for the PM domain in the following situations:
516 * (1) The domain is configured as always on.
517 * (2) When the domain has a subdomain being powered on.
519 if (genpd_is_always_on(genpd) ||
520 genpd_is_rpm_always_on(genpd) ||
521 atomic_read(&genpd->sd_count) > 0)
524 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
525 enum pm_qos_flags_status stat;
527 stat = dev_pm_qos_flags(pdd->dev, PM_QOS_FLAG_NO_POWER_OFF);
528 if (stat > PM_QOS_FLAGS_NONE)
532 * Do not allow PM domain to be powered off, when an IRQ safe
533 * device is part of a non-IRQ safe domain.
535 if (!pm_runtime_suspended(pdd->dev) ||
536 irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
540 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
543 if (genpd->gov && genpd->gov->power_down_ok) {
544 if (!genpd->gov->power_down_ok(&genpd->domain))
548 /* Default to shallowest state. */
550 genpd->state_idx = 0;
552 if (genpd->power_off) {
555 if (atomic_read(&genpd->sd_count) > 0)
559 * If sd_count > 0 at this point, one of the subdomains hasn't
560 * managed to call genpd_power_on() for the master yet after
561 * incrementing it. In that case genpd_power_on() will wait
562 * for us to drop the lock, so we can call .power_off() and let
563 * the genpd_power_on() restore power for us (this shouldn't
564 * happen very often).
566 ret = _genpd_power_off(genpd, true);
571 genpd->status = GPD_STATE_POWER_OFF;
572 genpd_update_accounting(genpd);
574 list_for_each_entry(link, &genpd->slave_links, slave_node) {
575 genpd_sd_counter_dec(link->master);
576 genpd_lock_nested(link->master, depth + 1);
577 genpd_power_off(link->master, false, depth + 1);
578 genpd_unlock(link->master);
585 * genpd_power_on - Restore power to a given PM domain and its masters.
586 * @genpd: PM domain to power up.
587 * @depth: nesting count for lockdep.
589 * Restore power to @genpd and all of its masters so that it is possible to
590 * resume a device belonging to it.
592 static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
594 struct gpd_link *link;
597 if (genpd_status_on(genpd))
601 * The list is guaranteed not to change while the loop below is being
602 * executed, unless one of the masters' .power_on() callbacks fiddles
605 list_for_each_entry(link, &genpd->slave_links, slave_node) {
606 struct generic_pm_domain *master = link->master;
608 genpd_sd_counter_inc(master);
610 genpd_lock_nested(master, depth + 1);
611 ret = genpd_power_on(master, depth + 1);
612 genpd_unlock(master);
615 genpd_sd_counter_dec(master);
620 ret = _genpd_power_on(genpd, true);
624 genpd->status = GPD_STATE_ACTIVE;
625 genpd_update_accounting(genpd);
630 list_for_each_entry_continue_reverse(link,
633 genpd_sd_counter_dec(link->master);
634 genpd_lock_nested(link->master, depth + 1);
635 genpd_power_off(link->master, false, depth + 1);
636 genpd_unlock(link->master);
642 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
643 unsigned long val, void *ptr)
645 struct generic_pm_domain_data *gpd_data;
648 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
649 dev = gpd_data->base.dev;
652 struct generic_pm_domain *genpd;
653 struct pm_domain_data *pdd;
655 spin_lock_irq(&dev->power.lock);
657 pdd = dev->power.subsys_data ?
658 dev->power.subsys_data->domain_data : NULL;
660 to_gpd_data(pdd)->td.constraint_changed = true;
661 genpd = dev_to_genpd(dev);
663 genpd = ERR_PTR(-ENODATA);
666 spin_unlock_irq(&dev->power.lock);
668 if (!IS_ERR(genpd)) {
670 genpd->max_off_time_changed = true;
675 if (!dev || dev->power.ignore_children)
683 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
684 * @work: Work structure used for scheduling the execution of this function.
686 static void genpd_power_off_work_fn(struct work_struct *work)
688 struct generic_pm_domain *genpd;
690 genpd = container_of(work, struct generic_pm_domain, power_off_work);
693 genpd_power_off(genpd, false, 0);
698 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
699 * @dev: Device to handle.
701 static int __genpd_runtime_suspend(struct device *dev)
703 int (*cb)(struct device *__dev);
705 if (dev->type && dev->type->pm)
706 cb = dev->type->pm->runtime_suspend;
707 else if (dev->class && dev->class->pm)
708 cb = dev->class->pm->runtime_suspend;
709 else if (dev->bus && dev->bus->pm)
710 cb = dev->bus->pm->runtime_suspend;
714 if (!cb && dev->driver && dev->driver->pm)
715 cb = dev->driver->pm->runtime_suspend;
717 return cb ? cb(dev) : 0;
721 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
722 * @dev: Device to handle.
724 static int __genpd_runtime_resume(struct device *dev)
726 int (*cb)(struct device *__dev);
728 if (dev->type && dev->type->pm)
729 cb = dev->type->pm->runtime_resume;
730 else if (dev->class && dev->class->pm)
731 cb = dev->class->pm->runtime_resume;
732 else if (dev->bus && dev->bus->pm)
733 cb = dev->bus->pm->runtime_resume;
737 if (!cb && dev->driver && dev->driver->pm)
738 cb = dev->driver->pm->runtime_resume;
740 return cb ? cb(dev) : 0;
744 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
745 * @dev: Device to suspend.
747 * Carry out a runtime suspend of a device under the assumption that its
748 * pm_domain field points to the domain member of an object of type
749 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
751 static int genpd_runtime_suspend(struct device *dev)
753 struct generic_pm_domain *genpd;
754 bool (*suspend_ok)(struct device *__dev);
755 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
756 bool runtime_pm = pm_runtime_enabled(dev);
761 dev_dbg(dev, "%s()\n", __func__);
763 genpd = dev_to_genpd(dev);
768 * A runtime PM centric subsystem/driver may re-use the runtime PM
769 * callbacks for other purposes than runtime PM. In those scenarios
770 * runtime PM is disabled. Under these circumstances, we shall skip
771 * validating/measuring the PM QoS latency.
773 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
774 if (runtime_pm && suspend_ok && !suspend_ok(dev))
777 /* Measure suspend latency. */
780 time_start = ktime_get();
782 ret = __genpd_runtime_suspend(dev);
786 ret = genpd_stop_dev(genpd, dev);
788 __genpd_runtime_resume(dev);
792 /* Update suspend latency value if the measured time exceeds it. */
794 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
795 if (elapsed_ns > td->suspend_latency_ns) {
796 td->suspend_latency_ns = elapsed_ns;
797 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
799 genpd->max_off_time_changed = true;
800 td->constraint_changed = true;
805 * If power.irq_safe is set, this routine may be run with
806 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
808 if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
812 genpd_power_off(genpd, true, 0);
819 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
820 * @dev: Device to resume.
822 * Carry out a runtime resume of a device under the assumption that its
823 * pm_domain field points to the domain member of an object of type
824 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
826 static int genpd_runtime_resume(struct device *dev)
828 struct generic_pm_domain *genpd;
829 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
830 bool runtime_pm = pm_runtime_enabled(dev);
836 dev_dbg(dev, "%s()\n", __func__);
838 genpd = dev_to_genpd(dev);
843 * As we don't power off a non IRQ safe domain, which holds
844 * an IRQ safe device, we don't need to restore power to it.
846 if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
852 ret = genpd_power_on(genpd, 0);
859 /* Measure resume latency. */
861 if (timed && runtime_pm)
862 time_start = ktime_get();
864 ret = genpd_start_dev(genpd, dev);
868 ret = __genpd_runtime_resume(dev);
872 /* Update resume latency value if the measured time exceeds it. */
873 if (timed && runtime_pm) {
874 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
875 if (elapsed_ns > td->resume_latency_ns) {
876 td->resume_latency_ns = elapsed_ns;
877 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
879 genpd->max_off_time_changed = true;
880 td->constraint_changed = true;
887 genpd_stop_dev(genpd, dev);
889 if (!pm_runtime_is_irq_safe(dev) ||
890 (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
892 genpd_power_off(genpd, true, 0);
899 static bool pd_ignore_unused;
900 static int __init pd_ignore_unused_setup(char *__unused)
902 pd_ignore_unused = true;
905 __setup("pd_ignore_unused", pd_ignore_unused_setup);
908 * genpd_power_off_unused - Power off all PM domains with no devices in use.
910 static int __init genpd_power_off_unused(void)
912 struct generic_pm_domain *genpd;
914 if (pd_ignore_unused) {
915 pr_warn("genpd: Not disabling unused power domains\n");
919 mutex_lock(&gpd_list_lock);
921 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
922 genpd_queue_power_off_work(genpd);
924 mutex_unlock(&gpd_list_lock);
928 late_initcall(genpd_power_off_unused);
930 #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
932 static bool genpd_present(const struct generic_pm_domain *genpd)
934 const struct generic_pm_domain *gpd;
936 if (IS_ERR_OR_NULL(genpd))
939 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
948 #ifdef CONFIG_PM_SLEEP
951 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
952 * @genpd: PM domain to power off, if possible.
953 * @use_lock: use the lock.
954 * @depth: nesting count for lockdep.
956 * Check if the given PM domain can be powered off (during system suspend or
957 * hibernation) and do that if so. Also, in that case propagate to its masters.
959 * This function is only called in "noirq" and "syscore" stages of system power
960 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
961 * these cases the lock must be held.
963 static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
966 struct gpd_link *link;
968 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
971 if (genpd->suspended_count != genpd->device_count
972 || atomic_read(&genpd->sd_count) > 0)
975 /* Choose the deepest state when suspending */
976 genpd->state_idx = genpd->state_count - 1;
977 if (_genpd_power_off(genpd, false))
980 genpd->status = GPD_STATE_POWER_OFF;
982 list_for_each_entry(link, &genpd->slave_links, slave_node) {
983 genpd_sd_counter_dec(link->master);
986 genpd_lock_nested(link->master, depth + 1);
988 genpd_sync_power_off(link->master, use_lock, depth + 1);
991 genpd_unlock(link->master);
996 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
997 * @genpd: PM domain to power on.
998 * @use_lock: use the lock.
999 * @depth: nesting count for lockdep.
1001 * This function is only called in "noirq" and "syscore" stages of system power
1002 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
1003 * these cases the lock must be held.
1005 static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
1008 struct gpd_link *link;
1010 if (genpd_status_on(genpd))
1013 list_for_each_entry(link, &genpd->slave_links, slave_node) {
1014 genpd_sd_counter_inc(link->master);
1017 genpd_lock_nested(link->master, depth + 1);
1019 genpd_sync_power_on(link->master, use_lock, depth + 1);
1022 genpd_unlock(link->master);
1025 _genpd_power_on(genpd, false);
1027 genpd->status = GPD_STATE_ACTIVE;
1031 * resume_needed - Check whether to resume a device before system suspend.
1032 * @dev: Device to check.
1033 * @genpd: PM domain the device belongs to.
1035 * There are two cases in which a device that can wake up the system from sleep
1036 * states should be resumed by genpd_prepare(): (1) if the device is enabled
1037 * to wake up the system and it has to remain active for this purpose while the
1038 * system is in the sleep state and (2) if the device is not enabled to wake up
1039 * the system from sleep states and it generally doesn't generate wakeup signals
1040 * by itself (those signals are generated on its behalf by other parts of the
1041 * system). In the latter case it may be necessary to reconfigure the device's
1042 * wakeup settings during system suspend, because it may have been set up to
1043 * signal remote wakeup from the system's working state as needed by runtime PM.
1044 * Return 'true' in either of the above cases.
1046 static bool resume_needed(struct device *dev,
1047 const struct generic_pm_domain *genpd)
1051 if (!device_can_wakeup(dev))
1054 active_wakeup = genpd_is_active_wakeup(genpd);
1055 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
1059 * genpd_prepare - Start power transition of a device in a PM domain.
1060 * @dev: Device to start the transition of.
1062 * Start a power transition of a device (during a system-wide power transition)
1063 * under the assumption that its pm_domain field points to the domain member of
1064 * an object of type struct generic_pm_domain representing a PM domain
1065 * consisting of I/O devices.
1067 static int genpd_prepare(struct device *dev)
1069 struct generic_pm_domain *genpd;
1072 dev_dbg(dev, "%s()\n", __func__);
1074 genpd = dev_to_genpd(dev);
1079 * If a wakeup request is pending for the device, it should be woken up
1080 * at this point and a system wakeup event should be reported if it's
1081 * set up to wake up the system from sleep states.
1083 if (resume_needed(dev, genpd))
1084 pm_runtime_resume(dev);
1088 if (genpd->prepared_count++ == 0)
1089 genpd->suspended_count = 0;
1091 genpd_unlock(genpd);
1093 ret = pm_generic_prepare(dev);
1097 genpd->prepared_count--;
1099 genpd_unlock(genpd);
1102 /* Never return 1, as genpd don't cope with the direct_complete path. */
1103 return ret >= 0 ? 0 : ret;
1107 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1109 * @dev: Device to suspend.
1110 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
1112 * Stop the device and remove power from the domain if all devices in it have
1115 static int genpd_finish_suspend(struct device *dev, bool poweroff)
1117 struct generic_pm_domain *genpd;
1120 genpd = dev_to_genpd(dev);
1125 ret = pm_generic_poweroff_noirq(dev);
1127 ret = pm_generic_suspend_noirq(dev);
1131 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1134 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1135 !pm_runtime_status_suspended(dev)) {
1136 ret = genpd_stop_dev(genpd, dev);
1139 pm_generic_restore_noirq(dev);
1141 pm_generic_resume_noirq(dev);
1147 genpd->suspended_count++;
1148 genpd_sync_power_off(genpd, true, 0);
1149 genpd_unlock(genpd);
1155 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1156 * @dev: Device to suspend.
1158 * Stop the device and remove power from the domain if all devices in it have
1161 static int genpd_suspend_noirq(struct device *dev)
1163 dev_dbg(dev, "%s()\n", __func__);
1165 return genpd_finish_suspend(dev, false);
1169 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1170 * @dev: Device to resume.
1172 * Restore power to the device's PM domain, if necessary, and start the device.
1174 static int genpd_resume_noirq(struct device *dev)
1176 struct generic_pm_domain *genpd;
1179 dev_dbg(dev, "%s()\n", __func__);
1181 genpd = dev_to_genpd(dev);
1185 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1186 return pm_generic_resume_noirq(dev);
1189 genpd_sync_power_on(genpd, true, 0);
1190 genpd->suspended_count--;
1191 genpd_unlock(genpd);
1193 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1194 !pm_runtime_status_suspended(dev)) {
1195 ret = genpd_start_dev(genpd, dev);
1200 return pm_generic_resume_noirq(dev);
1204 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1205 * @dev: Device to freeze.
1207 * Carry out a late freeze of a device under the assumption that its
1208 * pm_domain field points to the domain member of an object of type
1209 * struct generic_pm_domain representing a power domain consisting of I/O
1212 static int genpd_freeze_noirq(struct device *dev)
1214 const struct generic_pm_domain *genpd;
1217 dev_dbg(dev, "%s()\n", __func__);
1219 genpd = dev_to_genpd(dev);
1223 ret = pm_generic_freeze_noirq(dev);
1227 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1228 !pm_runtime_status_suspended(dev))
1229 ret = genpd_stop_dev(genpd, dev);
1235 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1236 * @dev: Device to thaw.
1238 * Start the device, unless power has been removed from the domain already
1239 * before the system transition.
1241 static int genpd_thaw_noirq(struct device *dev)
1243 const struct generic_pm_domain *genpd;
1246 dev_dbg(dev, "%s()\n", __func__);
1248 genpd = dev_to_genpd(dev);
1252 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1253 !pm_runtime_status_suspended(dev)) {
1254 ret = genpd_start_dev(genpd, dev);
1259 return pm_generic_thaw_noirq(dev);
1263 * genpd_poweroff_noirq - Completion of hibernation of device in an
1265 * @dev: Device to poweroff.
1267 * Stop the device and remove power from the domain if all devices in it have
1270 static int genpd_poweroff_noirq(struct device *dev)
1272 dev_dbg(dev, "%s()\n", __func__);
1274 return genpd_finish_suspend(dev, true);
1278 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1279 * @dev: Device to resume.
1281 * Make sure the domain will be in the same power state as before the
1282 * hibernation the system is resuming from and start the device if necessary.
1284 static int genpd_restore_noirq(struct device *dev)
1286 struct generic_pm_domain *genpd;
1289 dev_dbg(dev, "%s()\n", __func__);
1291 genpd = dev_to_genpd(dev);
1296 * At this point suspended_count == 0 means we are being run for the
1297 * first time for the given domain in the present cycle.
1300 if (genpd->suspended_count++ == 0)
1302 * The boot kernel might put the domain into arbitrary state,
1303 * so make it appear as powered off to genpd_sync_power_on(),
1304 * so that it tries to power it on in case it was really off.
1306 genpd->status = GPD_STATE_POWER_OFF;
1308 genpd_sync_power_on(genpd, true, 0);
1309 genpd_unlock(genpd);
1311 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1312 !pm_runtime_status_suspended(dev)) {
1313 ret = genpd_start_dev(genpd, dev);
1318 return pm_generic_restore_noirq(dev);
1322 * genpd_complete - Complete power transition of a device in a power domain.
1323 * @dev: Device to complete the transition of.
1325 * Complete a power transition of a device (during a system-wide power
1326 * transition) under the assumption that its pm_domain field points to the
1327 * domain member of an object of type struct generic_pm_domain representing
1328 * a power domain consisting of I/O devices.
1330 static void genpd_complete(struct device *dev)
1332 struct generic_pm_domain *genpd;
1334 dev_dbg(dev, "%s()\n", __func__);
1336 genpd = dev_to_genpd(dev);
1340 pm_generic_complete(dev);
1344 genpd->prepared_count--;
1345 if (!genpd->prepared_count)
1346 genpd_queue_power_off_work(genpd);
1348 genpd_unlock(genpd);
1352 * genpd_syscore_switch - Switch power during system core suspend or resume.
1353 * @dev: Device that normally is marked as "always on" to switch power for.
1355 * This routine may only be called during the system core (syscore) suspend or
1356 * resume phase for devices whose "always on" flags are set.
1358 static void genpd_syscore_switch(struct device *dev, bool suspend)
1360 struct generic_pm_domain *genpd;
1362 genpd = dev_to_genpd(dev);
1363 if (!genpd_present(genpd))
1367 genpd->suspended_count++;
1368 genpd_sync_power_off(genpd, false, 0);
1370 genpd_sync_power_on(genpd, false, 0);
1371 genpd->suspended_count--;
1375 void pm_genpd_syscore_poweroff(struct device *dev)
1377 genpd_syscore_switch(dev, true);
1379 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1381 void pm_genpd_syscore_poweron(struct device *dev)
1383 genpd_syscore_switch(dev, false);
1385 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1387 #else /* !CONFIG_PM_SLEEP */
1389 #define genpd_prepare NULL
1390 #define genpd_suspend_noirq NULL
1391 #define genpd_resume_noirq NULL
1392 #define genpd_freeze_noirq NULL
1393 #define genpd_thaw_noirq NULL
1394 #define genpd_poweroff_noirq NULL
1395 #define genpd_restore_noirq NULL
1396 #define genpd_complete NULL
1398 #endif /* CONFIG_PM_SLEEP */
1400 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev)
1402 struct generic_pm_domain_data *gpd_data;
1405 ret = dev_pm_get_subsys_data(dev);
1407 return ERR_PTR(ret);
1409 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1415 gpd_data->base.dev = dev;
1416 gpd_data->td.constraint_changed = true;
1417 gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
1418 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1420 spin_lock_irq(&dev->power.lock);
1422 if (dev->power.subsys_data->domain_data) {
1427 dev->power.subsys_data->domain_data = &gpd_data->base;
1429 spin_unlock_irq(&dev->power.lock);
1434 spin_unlock_irq(&dev->power.lock);
1437 dev_pm_put_subsys_data(dev);
1438 return ERR_PTR(ret);
1441 static void genpd_free_dev_data(struct device *dev,
1442 struct generic_pm_domain_data *gpd_data)
1444 spin_lock_irq(&dev->power.lock);
1446 dev->power.subsys_data->domain_data = NULL;
1448 spin_unlock_irq(&dev->power.lock);
1451 dev_pm_put_subsys_data(dev);
1454 static void genpd_update_cpumask(struct generic_pm_domain *genpd,
1455 int cpu, bool set, unsigned int depth)
1457 struct gpd_link *link;
1459 if (!genpd_is_cpu_domain(genpd))
1462 list_for_each_entry(link, &genpd->slave_links, slave_node) {
1463 struct generic_pm_domain *master = link->master;
1465 genpd_lock_nested(master, depth + 1);
1466 genpd_update_cpumask(master, cpu, set, depth + 1);
1467 genpd_unlock(master);
1471 cpumask_set_cpu(cpu, genpd->cpus);
1473 cpumask_clear_cpu(cpu, genpd->cpus);
1476 static void genpd_set_cpumask(struct generic_pm_domain *genpd, int cpu)
1479 genpd_update_cpumask(genpd, cpu, true, 0);
1482 static void genpd_clear_cpumask(struct generic_pm_domain *genpd, int cpu)
1485 genpd_update_cpumask(genpd, cpu, false, 0);
1488 static int genpd_get_cpu(struct generic_pm_domain *genpd, struct device *dev)
1492 if (!genpd_is_cpu_domain(genpd))
1495 for_each_possible_cpu(cpu) {
1496 if (get_cpu_device(cpu) == dev)
1503 static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1504 struct device *base_dev)
1506 struct generic_pm_domain_data *gpd_data;
1509 dev_dbg(dev, "%s()\n", __func__);
1511 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1514 gpd_data = genpd_alloc_dev_data(dev);
1515 if (IS_ERR(gpd_data))
1516 return PTR_ERR(gpd_data);
1518 gpd_data->cpu = genpd_get_cpu(genpd, base_dev);
1520 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1526 genpd_set_cpumask(genpd, gpd_data->cpu);
1527 dev_pm_domain_set(dev, &genpd->domain);
1529 genpd->device_count++;
1530 genpd->max_off_time_changed = true;
1532 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1534 genpd_unlock(genpd);
1537 genpd_free_dev_data(dev, gpd_data);
1539 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1545 * pm_genpd_add_device - Add a device to an I/O PM domain.
1546 * @genpd: PM domain to add the device to.
1547 * @dev: Device to be added.
1549 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1553 mutex_lock(&gpd_list_lock);
1554 ret = genpd_add_device(genpd, dev, dev);
1555 mutex_unlock(&gpd_list_lock);
1559 EXPORT_SYMBOL_GPL(pm_genpd_add_device);
1561 static int genpd_remove_device(struct generic_pm_domain *genpd,
1564 struct generic_pm_domain_data *gpd_data;
1565 struct pm_domain_data *pdd;
1568 dev_dbg(dev, "%s()\n", __func__);
1570 pdd = dev->power.subsys_data->domain_data;
1571 gpd_data = to_gpd_data(pdd);
1572 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1576 if (genpd->prepared_count > 0) {
1581 genpd->device_count--;
1582 genpd->max_off_time_changed = true;
1584 genpd_clear_cpumask(genpd, gpd_data->cpu);
1585 dev_pm_domain_set(dev, NULL);
1587 list_del_init(&pdd->list_node);
1589 genpd_unlock(genpd);
1591 if (genpd->detach_dev)
1592 genpd->detach_dev(genpd, dev);
1594 genpd_free_dev_data(dev, gpd_data);
1599 genpd_unlock(genpd);
1600 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1606 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1607 * @dev: Device to be removed.
1609 int pm_genpd_remove_device(struct device *dev)
1611 struct generic_pm_domain *genpd = genpd_lookup_dev(dev);
1616 return genpd_remove_device(genpd, dev);
1618 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1620 static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1621 struct generic_pm_domain *subdomain)
1623 struct gpd_link *link, *itr;
1626 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1627 || genpd == subdomain)
1631 * If the domain can be powered on/off in an IRQ safe
1632 * context, ensure that the subdomain can also be
1633 * powered on/off in that context.
1635 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1636 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1637 genpd->name, subdomain->name);
1641 link = kzalloc(sizeof(*link), GFP_KERNEL);
1645 genpd_lock(subdomain);
1646 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1648 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1653 list_for_each_entry(itr, &genpd->master_links, master_node) {
1654 if (itr->slave == subdomain && itr->master == genpd) {
1660 link->master = genpd;
1661 list_add_tail(&link->master_node, &genpd->master_links);
1662 link->slave = subdomain;
1663 list_add_tail(&link->slave_node, &subdomain->slave_links);
1664 if (genpd_status_on(subdomain))
1665 genpd_sd_counter_inc(genpd);
1668 genpd_unlock(genpd);
1669 genpd_unlock(subdomain);
1676 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1677 * @genpd: Master PM domain to add the subdomain to.
1678 * @subdomain: Subdomain to be added.
1680 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1681 struct generic_pm_domain *subdomain)
1685 mutex_lock(&gpd_list_lock);
1686 ret = genpd_add_subdomain(genpd, subdomain);
1687 mutex_unlock(&gpd_list_lock);
1691 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1694 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1695 * @genpd: Master PM domain to remove the subdomain from.
1696 * @subdomain: Subdomain to be removed.
1698 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1699 struct generic_pm_domain *subdomain)
1701 struct gpd_link *l, *link;
1704 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1707 genpd_lock(subdomain);
1708 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1710 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1711 pr_warn("%s: unable to remove subdomain %s\n",
1712 genpd->name, subdomain->name);
1717 list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
1718 if (link->slave != subdomain)
1721 list_del(&link->master_node);
1722 list_del(&link->slave_node);
1724 if (genpd_status_on(subdomain))
1725 genpd_sd_counter_dec(genpd);
1732 genpd_unlock(genpd);
1733 genpd_unlock(subdomain);
1737 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1739 static void genpd_free_default_power_state(struct genpd_power_state *states,
1740 unsigned int state_count)
1745 static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1747 struct genpd_power_state *state;
1749 state = kzalloc(sizeof(*state), GFP_KERNEL);
1753 genpd->states = state;
1754 genpd->state_count = 1;
1755 genpd->free_states = genpd_free_default_power_state;
1760 static void genpd_lock_init(struct generic_pm_domain *genpd)
1762 if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1763 spin_lock_init(&genpd->slock);
1764 genpd->lock_ops = &genpd_spin_ops;
1766 mutex_init(&genpd->mlock);
1767 genpd->lock_ops = &genpd_mtx_ops;
1772 * pm_genpd_init - Initialize a generic I/O PM domain object.
1773 * @genpd: PM domain object to initialize.
1774 * @gov: PM domain governor to associate with the domain (may be NULL).
1775 * @is_off: Initial value of the domain's power_is_off field.
1777 * Returns 0 on successful initialization, else a negative error code.
1779 int pm_genpd_init(struct generic_pm_domain *genpd,
1780 struct dev_power_governor *gov, bool is_off)
1784 if (IS_ERR_OR_NULL(genpd))
1787 INIT_LIST_HEAD(&genpd->master_links);
1788 INIT_LIST_HEAD(&genpd->slave_links);
1789 INIT_LIST_HEAD(&genpd->dev_list);
1790 genpd_lock_init(genpd);
1792 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1793 atomic_set(&genpd->sd_count, 0);
1794 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1795 genpd->device_count = 0;
1796 genpd->max_off_time_ns = -1;
1797 genpd->max_off_time_changed = true;
1798 genpd->provider = NULL;
1799 genpd->has_provider = false;
1800 genpd->accounting_time = ktime_get();
1801 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1802 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
1803 genpd->domain.ops.prepare = genpd_prepare;
1804 genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
1805 genpd->domain.ops.resume_noirq = genpd_resume_noirq;
1806 genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
1807 genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
1808 genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
1809 genpd->domain.ops.restore_noirq = genpd_restore_noirq;
1810 genpd->domain.ops.complete = genpd_complete;
1812 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1813 genpd->dev_ops.stop = pm_clk_suspend;
1814 genpd->dev_ops.start = pm_clk_resume;
1817 /* Always-on domains must be powered on at initialization. */
1818 if ((genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd)) &&
1819 !genpd_status_on(genpd))
1822 if (genpd_is_cpu_domain(genpd) &&
1823 !zalloc_cpumask_var(&genpd->cpus, GFP_KERNEL))
1826 /* Use only one "off" state if there were no states declared */
1827 if (genpd->state_count == 0) {
1828 ret = genpd_set_default_power_state(genpd);
1830 if (genpd_is_cpu_domain(genpd))
1831 free_cpumask_var(genpd->cpus);
1834 } else if (!gov && genpd->state_count > 1) {
1835 pr_warn("%s: no governor for states\n", genpd->name);
1838 device_initialize(&genpd->dev);
1839 dev_set_name(&genpd->dev, "%s", genpd->name);
1841 mutex_lock(&gpd_list_lock);
1842 list_add(&genpd->gpd_list_node, &gpd_list);
1843 mutex_unlock(&gpd_list_lock);
1847 EXPORT_SYMBOL_GPL(pm_genpd_init);
1849 static int genpd_remove(struct generic_pm_domain *genpd)
1851 struct gpd_link *l, *link;
1853 if (IS_ERR_OR_NULL(genpd))
1858 if (genpd->has_provider) {
1859 genpd_unlock(genpd);
1860 pr_err("Provider present, unable to remove %s\n", genpd->name);
1864 if (!list_empty(&genpd->master_links) || genpd->device_count) {
1865 genpd_unlock(genpd);
1866 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1870 list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
1871 list_del(&link->master_node);
1872 list_del(&link->slave_node);
1876 list_del(&genpd->gpd_list_node);
1877 genpd_unlock(genpd);
1878 cancel_work_sync(&genpd->power_off_work);
1879 if (genpd_is_cpu_domain(genpd))
1880 free_cpumask_var(genpd->cpus);
1881 if (genpd->free_states)
1882 genpd->free_states(genpd->states, genpd->state_count);
1884 pr_debug("%s: removed %s\n", __func__, genpd->name);
1890 * pm_genpd_remove - Remove a generic I/O PM domain
1891 * @genpd: Pointer to PM domain that is to be removed.
1893 * To remove the PM domain, this function:
1894 * - Removes the PM domain as a subdomain to any parent domains,
1896 * - Removes the PM domain from the list of registered PM domains.
1898 * The PM domain will only be removed, if the associated provider has
1899 * been removed, it is not a parent to any other PM domain and has no
1900 * devices associated with it.
1902 int pm_genpd_remove(struct generic_pm_domain *genpd)
1906 mutex_lock(&gpd_list_lock);
1907 ret = genpd_remove(genpd);
1908 mutex_unlock(&gpd_list_lock);
1912 EXPORT_SYMBOL_GPL(pm_genpd_remove);
1914 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1917 * Device Tree based PM domain providers.
1919 * The code below implements generic device tree based PM domain providers that
1920 * bind device tree nodes with generic PM domains registered in the system.
1922 * Any driver that registers generic PM domains and needs to support binding of
1923 * devices to these domains is supposed to register a PM domain provider, which
1924 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1926 * Two simple mapping functions have been provided for convenience:
1927 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1928 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1933 * struct of_genpd_provider - PM domain provider registration structure
1934 * @link: Entry in global list of PM domain providers
1935 * @node: Pointer to device tree node of PM domain provider
1936 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1938 * @data: context pointer to be passed into @xlate callback
1940 struct of_genpd_provider {
1941 struct list_head link;
1942 struct device_node *node;
1943 genpd_xlate_t xlate;
1947 /* List of registered PM domain providers. */
1948 static LIST_HEAD(of_genpd_providers);
1949 /* Mutex to protect the list above. */
1950 static DEFINE_MUTEX(of_genpd_mutex);
1953 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1954 * @genpdspec: OF phandle args to map into a PM domain
1955 * @data: xlate function private data - pointer to struct generic_pm_domain
1957 * This is a generic xlate function that can be used to model PM domains that
1958 * have their own device tree nodes. The private data of xlate function needs
1959 * to be a valid pointer to struct generic_pm_domain.
1961 static struct generic_pm_domain *genpd_xlate_simple(
1962 struct of_phandle_args *genpdspec,
1969 * genpd_xlate_onecell() - Xlate function using a single index.
1970 * @genpdspec: OF phandle args to map into a PM domain
1971 * @data: xlate function private data - pointer to struct genpd_onecell_data
1973 * This is a generic xlate function that can be used to model simple PM domain
1974 * controllers that have one device tree node and provide multiple PM domains.
1975 * A single cell is used as an index into an array of PM domains specified in
1976 * the genpd_onecell_data struct when registering the provider.
1978 static struct generic_pm_domain *genpd_xlate_onecell(
1979 struct of_phandle_args *genpdspec,
1982 struct genpd_onecell_data *genpd_data = data;
1983 unsigned int idx = genpdspec->args[0];
1985 if (genpdspec->args_count != 1)
1986 return ERR_PTR(-EINVAL);
1988 if (idx >= genpd_data->num_domains) {
1989 pr_err("%s: invalid domain index %u\n", __func__, idx);
1990 return ERR_PTR(-EINVAL);
1993 if (!genpd_data->domains[idx])
1994 return ERR_PTR(-ENOENT);
1996 return genpd_data->domains[idx];
2000 * genpd_add_provider() - Register a PM domain provider for a node
2001 * @np: Device node pointer associated with the PM domain provider.
2002 * @xlate: Callback for decoding PM domain from phandle arguments.
2003 * @data: Context pointer for @xlate callback.
2005 static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
2008 struct of_genpd_provider *cp;
2010 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2014 cp->node = of_node_get(np);
2018 mutex_lock(&of_genpd_mutex);
2019 list_add(&cp->link, &of_genpd_providers);
2020 mutex_unlock(&of_genpd_mutex);
2021 pr_debug("Added domain provider from %pOF\n", np);
2027 * of_genpd_add_provider_simple() - Register a simple PM domain provider
2028 * @np: Device node pointer associated with the PM domain provider.
2029 * @genpd: Pointer to PM domain associated with the PM domain provider.
2031 int of_genpd_add_provider_simple(struct device_node *np,
2032 struct generic_pm_domain *genpd)
2039 mutex_lock(&gpd_list_lock);
2041 if (!genpd_present(genpd))
2044 genpd->dev.of_node = np;
2046 /* Parse genpd OPP table */
2047 if (genpd->set_performance_state) {
2048 ret = dev_pm_opp_of_add_table(&genpd->dev);
2050 dev_err(&genpd->dev, "Failed to add OPP table: %d\n",
2056 * Save table for faster processing while setting performance
2059 genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
2060 WARN_ON(!genpd->opp_table);
2063 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
2065 if (genpd->set_performance_state) {
2066 dev_pm_opp_put_opp_table(genpd->opp_table);
2067 dev_pm_opp_of_remove_table(&genpd->dev);
2073 genpd->provider = &np->fwnode;
2074 genpd->has_provider = true;
2077 mutex_unlock(&gpd_list_lock);
2081 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
2084 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
2085 * @np: Device node pointer associated with the PM domain provider.
2086 * @data: Pointer to the data associated with the PM domain provider.
2088 int of_genpd_add_provider_onecell(struct device_node *np,
2089 struct genpd_onecell_data *data)
2091 struct generic_pm_domain *genpd;
2098 mutex_lock(&gpd_list_lock);
2101 data->xlate = genpd_xlate_onecell;
2103 for (i = 0; i < data->num_domains; i++) {
2104 genpd = data->domains[i];
2108 if (!genpd_present(genpd))
2111 genpd->dev.of_node = np;
2113 /* Parse genpd OPP table */
2114 if (genpd->set_performance_state) {
2115 ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
2117 dev_err(&genpd->dev, "Failed to add OPP table for index %d: %d\n",
2123 * Save table for faster processing while setting
2124 * performance state.
2126 genpd->opp_table = dev_pm_opp_get_opp_table_indexed(&genpd->dev, i);
2127 WARN_ON(!genpd->opp_table);
2130 genpd->provider = &np->fwnode;
2131 genpd->has_provider = true;
2134 ret = genpd_add_provider(np, data->xlate, data);
2138 mutex_unlock(&gpd_list_lock);
2144 genpd = data->domains[i];
2149 genpd->provider = NULL;
2150 genpd->has_provider = false;
2152 if (genpd->set_performance_state) {
2153 dev_pm_opp_put_opp_table(genpd->opp_table);
2154 dev_pm_opp_of_remove_table(&genpd->dev);
2158 mutex_unlock(&gpd_list_lock);
2162 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
2165 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2166 * @np: Device node pointer associated with the PM domain provider
2168 void of_genpd_del_provider(struct device_node *np)
2170 struct of_genpd_provider *cp, *tmp;
2171 struct generic_pm_domain *gpd;
2173 mutex_lock(&gpd_list_lock);
2174 mutex_lock(&of_genpd_mutex);
2175 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
2176 if (cp->node == np) {
2178 * For each PM domain associated with the
2179 * provider, set the 'has_provider' to false
2180 * so that the PM domain can be safely removed.
2182 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2183 if (gpd->provider == &np->fwnode) {
2184 gpd->has_provider = false;
2186 if (!gpd->set_performance_state)
2189 dev_pm_opp_put_opp_table(gpd->opp_table);
2190 dev_pm_opp_of_remove_table(&gpd->dev);
2194 list_del(&cp->link);
2195 of_node_put(cp->node);
2200 mutex_unlock(&of_genpd_mutex);
2201 mutex_unlock(&gpd_list_lock);
2203 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2206 * genpd_get_from_provider() - Look-up PM domain
2207 * @genpdspec: OF phandle args to use for look-up
2209 * Looks for a PM domain provider under the node specified by @genpdspec and if
2210 * found, uses xlate function of the provider to map phandle args to a PM
2213 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2216 static struct generic_pm_domain *genpd_get_from_provider(
2217 struct of_phandle_args *genpdspec)
2219 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2220 struct of_genpd_provider *provider;
2223 return ERR_PTR(-EINVAL);
2225 mutex_lock(&of_genpd_mutex);
2227 /* Check if we have such a provider in our array */
2228 list_for_each_entry(provider, &of_genpd_providers, link) {
2229 if (provider->node == genpdspec->np)
2230 genpd = provider->xlate(genpdspec, provider->data);
2235 mutex_unlock(&of_genpd_mutex);
2241 * of_genpd_add_device() - Add a device to an I/O PM domain
2242 * @genpdspec: OF phandle args to use for look-up PM domain
2243 * @dev: Device to be added.
2245 * Looks-up an I/O PM domain based upon phandle args provided and adds
2246 * the device to the PM domain. Returns a negative error code on failure.
2248 int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
2250 struct generic_pm_domain *genpd;
2253 mutex_lock(&gpd_list_lock);
2255 genpd = genpd_get_from_provider(genpdspec);
2256 if (IS_ERR(genpd)) {
2257 ret = PTR_ERR(genpd);
2261 ret = genpd_add_device(genpd, dev, dev);
2264 mutex_unlock(&gpd_list_lock);
2268 EXPORT_SYMBOL_GPL(of_genpd_add_device);
2271 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2272 * @parent_spec: OF phandle args to use for parent PM domain look-up
2273 * @subdomain_spec: OF phandle args to use for subdomain look-up
2275 * Looks-up a parent PM domain and subdomain based upon phandle args
2276 * provided and adds the subdomain to the parent PM domain. Returns a
2277 * negative error code on failure.
2279 int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
2280 struct of_phandle_args *subdomain_spec)
2282 struct generic_pm_domain *parent, *subdomain;
2285 mutex_lock(&gpd_list_lock);
2287 parent = genpd_get_from_provider(parent_spec);
2288 if (IS_ERR(parent)) {
2289 ret = PTR_ERR(parent);
2293 subdomain = genpd_get_from_provider(subdomain_spec);
2294 if (IS_ERR(subdomain)) {
2295 ret = PTR_ERR(subdomain);
2299 ret = genpd_add_subdomain(parent, subdomain);
2302 mutex_unlock(&gpd_list_lock);
2306 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2309 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2310 * @provider: Pointer to device structure associated with provider
2312 * Find the last PM domain that was added by a particular provider and
2313 * remove this PM domain from the list of PM domains. The provider is
2314 * identified by the 'provider' device structure that is passed. The PM
2315 * domain will only be removed, if the provider associated with domain
2318 * Returns a valid pointer to struct generic_pm_domain on success or
2319 * ERR_PTR() on failure.
2321 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2323 struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2326 if (IS_ERR_OR_NULL(np))
2327 return ERR_PTR(-EINVAL);
2329 mutex_lock(&gpd_list_lock);
2330 list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2331 if (gpd->provider == &np->fwnode) {
2332 ret = genpd_remove(gpd);
2333 genpd = ret ? ERR_PTR(ret) : gpd;
2337 mutex_unlock(&gpd_list_lock);
2341 EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2343 static void genpd_release_dev(struct device *dev)
2345 of_node_put(dev->of_node);
2349 static struct bus_type genpd_bus_type = {
2354 * genpd_dev_pm_detach - Detach a device from its PM domain.
2355 * @dev: Device to detach.
2356 * @power_off: Currently not used
2358 * Try to locate a corresponding generic PM domain, which the device was
2359 * attached to previously. If such is found, the device is detached from it.
2361 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2363 struct generic_pm_domain *pd;
2367 pd = dev_to_genpd(dev);
2371 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2373 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2374 ret = genpd_remove_device(pd, dev);
2383 dev_err(dev, "failed to remove from PM domain %s: %d",
2388 /* Check if PM domain can be powered off after removing this device. */
2389 genpd_queue_power_off_work(pd);
2391 /* Unregister the device if it was created by genpd. */
2392 if (dev->bus == &genpd_bus_type)
2393 device_unregister(dev);
2396 static void genpd_dev_pm_sync(struct device *dev)
2398 struct generic_pm_domain *pd;
2400 pd = dev_to_genpd(dev);
2404 genpd_queue_power_off_work(pd);
2407 static int __genpd_dev_pm_attach(struct device *dev, struct device *base_dev,
2408 unsigned int index, bool power_on)
2410 struct of_phandle_args pd_args;
2411 struct generic_pm_domain *pd;
2414 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2415 "#power-domain-cells", index, &pd_args);
2419 mutex_lock(&gpd_list_lock);
2420 pd = genpd_get_from_provider(&pd_args);
2421 of_node_put(pd_args.np);
2423 mutex_unlock(&gpd_list_lock);
2424 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2425 __func__, PTR_ERR(pd));
2426 return driver_deferred_probe_check_state(base_dev);
2429 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2431 ret = genpd_add_device(pd, dev, base_dev);
2432 mutex_unlock(&gpd_list_lock);
2435 if (ret != -EPROBE_DEFER)
2436 dev_err(dev, "failed to add to PM domain %s: %d",
2441 dev->pm_domain->detach = genpd_dev_pm_detach;
2442 dev->pm_domain->sync = genpd_dev_pm_sync;
2446 ret = genpd_power_on(pd, 0);
2451 genpd_remove_device(pd, dev);
2453 return ret ? -EPROBE_DEFER : 1;
2457 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2458 * @dev: Device to attach.
2460 * Parse device's OF node to find a PM domain specifier. If such is found,
2461 * attaches the device to retrieved pm_domain ops.
2463 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2464 * PM domain or when multiple power-domains exists for it, else a negative error
2465 * code. Note that if a power-domain exists for the device, but it cannot be
2466 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2467 * not probed and to re-try again later.
2469 int genpd_dev_pm_attach(struct device *dev)
2475 * Devices with multiple PM domains must be attached separately, as we
2476 * can only attach one PM domain per device.
2478 if (of_count_phandle_with_args(dev->of_node, "power-domains",
2479 "#power-domain-cells") != 1)
2482 return __genpd_dev_pm_attach(dev, dev, 0, true);
2484 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2487 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2488 * @dev: The device used to lookup the PM domain.
2489 * @index: The index of the PM domain.
2491 * Parse device's OF node to find a PM domain specifier at the provided @index.
2492 * If such is found, creates a virtual device and attaches it to the retrieved
2493 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2494 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2496 * Returns the created virtual device if successfully attached PM domain, NULL
2497 * when the device don't need a PM domain, else an ERR_PTR() in case of
2498 * failures. If a power-domain exists for the device, but cannot be found or
2499 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2500 * is not probed and to re-try again later.
2502 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
2505 struct device *virt_dev;
2512 /* Verify that the index is within a valid range. */
2513 num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
2514 "#power-domain-cells");
2515 if (index >= num_domains)
2518 /* Allocate and register device on the genpd bus. */
2519 virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL);
2521 return ERR_PTR(-ENOMEM);
2523 dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev));
2524 virt_dev->bus = &genpd_bus_type;
2525 virt_dev->release = genpd_release_dev;
2526 virt_dev->of_node = of_node_get(dev->of_node);
2528 ret = device_register(virt_dev);
2530 put_device(virt_dev);
2531 return ERR_PTR(ret);
2534 /* Try to attach the device to the PM domain at the specified index. */
2535 ret = __genpd_dev_pm_attach(virt_dev, dev, index, false);
2537 device_unregister(virt_dev);
2538 return ret ? ERR_PTR(ret) : NULL;
2541 pm_runtime_enable(virt_dev);
2542 genpd_queue_power_off_work(dev_to_genpd(virt_dev));
2546 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
2549 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
2550 * @dev: The device used to lookup the PM domain.
2551 * @name: The name of the PM domain.
2553 * Parse device's OF node to find a PM domain specifier using the
2554 * power-domain-names DT property. For further description see
2555 * genpd_dev_pm_attach_by_id().
2557 struct device *genpd_dev_pm_attach_by_name(struct device *dev, const char *name)
2564 index = of_property_match_string(dev->of_node, "power-domain-names",
2569 return genpd_dev_pm_attach_by_id(dev, index);
2572 static const struct of_device_id idle_state_match[] = {
2573 { .compatible = "domain-idle-state", },
2577 static int genpd_parse_state(struct genpd_power_state *genpd_state,
2578 struct device_node *state_node)
2582 u32 entry_latency, exit_latency;
2584 err = of_property_read_u32(state_node, "entry-latency-us",
2587 pr_debug(" * %pOF missing entry-latency-us property\n",
2592 err = of_property_read_u32(state_node, "exit-latency-us",
2595 pr_debug(" * %pOF missing exit-latency-us property\n",
2600 err = of_property_read_u32(state_node, "min-residency-us", &residency);
2602 genpd_state->residency_ns = 1000 * residency;
2604 genpd_state->power_on_latency_ns = 1000 * exit_latency;
2605 genpd_state->power_off_latency_ns = 1000 * entry_latency;
2606 genpd_state->fwnode = &state_node->fwnode;
2611 static int genpd_iterate_idle_states(struct device_node *dn,
2612 struct genpd_power_state *states)
2615 struct of_phandle_iterator it;
2616 struct device_node *np;
2619 ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2623 /* Loop over the phandles until all the requested entry is found */
2624 of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
2626 if (!of_match_node(idle_state_match, np))
2629 ret = genpd_parse_state(&states[i], np);
2631 pr_err("Parsing idle state node %pOF failed with err %d\n",
2644 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2646 * @dn: The genpd device node
2647 * @states: The pointer to which the state array will be saved.
2648 * @n: The count of elements in the array returned from this function.
2650 * Returns the device states parsed from the OF node. The memory for the states
2651 * is allocated by this function and is the responsibility of the caller to
2652 * free the memory after use. If any or zero compatible domain idle states is
2653 * found it returns 0 and in case of errors, a negative error code is returned.
2655 int of_genpd_parse_idle_states(struct device_node *dn,
2656 struct genpd_power_state **states, int *n)
2658 struct genpd_power_state *st;
2661 ret = genpd_iterate_idle_states(dn, NULL);
2671 st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
2675 ret = genpd_iterate_idle_states(dn, st);
2678 return ret < 0 ? ret : -EINVAL;
2686 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2689 * pm_genpd_opp_to_performance_state - Gets performance state of the genpd from its OPP node.
2691 * @genpd_dev: Genpd's device for which the performance-state needs to be found.
2692 * @opp: struct dev_pm_opp of the OPP for which we need to find performance
2695 * Returns performance state encoded in the OPP of the genpd. This calls
2696 * platform specific genpd->opp_to_performance_state() callback to translate
2697 * power domain OPP to performance state.
2699 * Returns performance state on success and 0 on failure.
2701 unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
2702 struct dev_pm_opp *opp)
2704 struct generic_pm_domain *genpd = NULL;
2707 genpd = container_of(genpd_dev, struct generic_pm_domain, dev);
2709 if (unlikely(!genpd->opp_to_performance_state))
2713 state = genpd->opp_to_performance_state(genpd, opp);
2714 genpd_unlock(genpd);
2718 EXPORT_SYMBOL_GPL(pm_genpd_opp_to_performance_state);
2720 static int __init genpd_bus_init(void)
2722 return bus_register(&genpd_bus_type);
2724 core_initcall(genpd_bus_init);
2726 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2729 /*** debugfs support ***/
2731 #ifdef CONFIG_DEBUG_FS
2732 #include <linux/pm.h>
2733 #include <linux/device.h>
2734 #include <linux/debugfs.h>
2735 #include <linux/seq_file.h>
2736 #include <linux/init.h>
2737 #include <linux/kobject.h>
2738 static struct dentry *genpd_debugfs_dir;
2741 * TODO: This function is a slightly modified version of rtpm_status_show
2742 * from sysfs.c, so generalize it.
2744 static void rtpm_status_str(struct seq_file *s, struct device *dev)
2746 static const char * const status_lookup[] = {
2747 [RPM_ACTIVE] = "active",
2748 [RPM_RESUMING] = "resuming",
2749 [RPM_SUSPENDED] = "suspended",
2750 [RPM_SUSPENDING] = "suspending"
2754 if (dev->power.runtime_error)
2756 else if (dev->power.disable_depth)
2758 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2759 p = status_lookup[dev->power.runtime_status];
2766 static int genpd_summary_one(struct seq_file *s,
2767 struct generic_pm_domain *genpd)
2769 static const char * const status_lookup[] = {
2770 [GPD_STATE_ACTIVE] = "on",
2771 [GPD_STATE_POWER_OFF] = "off"
2773 struct pm_domain_data *pm_data;
2774 const char *kobj_path;
2775 struct gpd_link *link;
2779 ret = genpd_lock_interruptible(genpd);
2781 return -ERESTARTSYS;
2783 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2785 if (!genpd_status_on(genpd))
2786 snprintf(state, sizeof(state), "%s-%u",
2787 status_lookup[genpd->status], genpd->state_idx);
2789 snprintf(state, sizeof(state), "%s",
2790 status_lookup[genpd->status]);
2791 seq_printf(s, "%-30s %-15s ", genpd->name, state);
2794 * Modifications on the list require holding locks on both
2795 * master and slave, so we are safe.
2796 * Also genpd->name is immutable.
2798 list_for_each_entry(link, &genpd->master_links, master_node) {
2799 seq_printf(s, "%s", link->slave->name);
2800 if (!list_is_last(&link->master_node, &genpd->master_links))
2804 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2805 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2806 genpd_is_irq_safe(genpd) ?
2807 GFP_ATOMIC : GFP_KERNEL);
2808 if (kobj_path == NULL)
2811 seq_printf(s, "\n %-50s ", kobj_path);
2812 rtpm_status_str(s, pm_data->dev);
2818 genpd_unlock(genpd);
2823 static int summary_show(struct seq_file *s, void *data)
2825 struct generic_pm_domain *genpd;
2828 seq_puts(s, "domain status slaves\n");
2829 seq_puts(s, " /device runtime status\n");
2830 seq_puts(s, "----------------------------------------------------------------------\n");
2832 ret = mutex_lock_interruptible(&gpd_list_lock);
2834 return -ERESTARTSYS;
2836 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2837 ret = genpd_summary_one(s, genpd);
2841 mutex_unlock(&gpd_list_lock);
2846 static int status_show(struct seq_file *s, void *data)
2848 static const char * const status_lookup[] = {
2849 [GPD_STATE_ACTIVE] = "on",
2850 [GPD_STATE_POWER_OFF] = "off"
2853 struct generic_pm_domain *genpd = s->private;
2856 ret = genpd_lock_interruptible(genpd);
2858 return -ERESTARTSYS;
2860 if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
2863 if (genpd->status == GPD_STATE_POWER_OFF)
2864 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
2867 seq_printf(s, "%s\n", status_lookup[genpd->status]);
2869 genpd_unlock(genpd);
2873 static int sub_domains_show(struct seq_file *s, void *data)
2875 struct generic_pm_domain *genpd = s->private;
2876 struct gpd_link *link;
2879 ret = genpd_lock_interruptible(genpd);
2881 return -ERESTARTSYS;
2883 list_for_each_entry(link, &genpd->master_links, master_node)
2884 seq_printf(s, "%s\n", link->slave->name);
2886 genpd_unlock(genpd);
2890 static int idle_states_show(struct seq_file *s, void *data)
2892 struct generic_pm_domain *genpd = s->private;
2896 ret = genpd_lock_interruptible(genpd);
2898 return -ERESTARTSYS;
2900 seq_puts(s, "State Time Spent(ms)\n");
2902 for (i = 0; i < genpd->state_count; i++) {
2906 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2907 (genpd->state_idx == i))
2908 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2910 msecs = ktime_to_ms(
2911 ktime_add(genpd->states[i].idle_time, delta));
2912 seq_printf(s, "S%-13i %lld\n", i, msecs);
2915 genpd_unlock(genpd);
2919 static int active_time_show(struct seq_file *s, void *data)
2921 struct generic_pm_domain *genpd = s->private;
2925 ret = genpd_lock_interruptible(genpd);
2927 return -ERESTARTSYS;
2929 if (genpd->status == GPD_STATE_ACTIVE)
2930 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2932 seq_printf(s, "%lld ms\n", ktime_to_ms(
2933 ktime_add(genpd->on_time, delta)));
2935 genpd_unlock(genpd);
2939 static int total_idle_time_show(struct seq_file *s, void *data)
2941 struct generic_pm_domain *genpd = s->private;
2942 ktime_t delta = 0, total = 0;
2946 ret = genpd_lock_interruptible(genpd);
2948 return -ERESTARTSYS;
2950 for (i = 0; i < genpd->state_count; i++) {
2952 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2953 (genpd->state_idx == i))
2954 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2956 total = ktime_add(total, genpd->states[i].idle_time);
2958 total = ktime_add(total, delta);
2960 seq_printf(s, "%lld ms\n", ktime_to_ms(total));
2962 genpd_unlock(genpd);
2967 static int devices_show(struct seq_file *s, void *data)
2969 struct generic_pm_domain *genpd = s->private;
2970 struct pm_domain_data *pm_data;
2971 const char *kobj_path;
2974 ret = genpd_lock_interruptible(genpd);
2976 return -ERESTARTSYS;
2978 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2979 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2980 genpd_is_irq_safe(genpd) ?
2981 GFP_ATOMIC : GFP_KERNEL);
2982 if (kobj_path == NULL)
2985 seq_printf(s, "%s\n", kobj_path);
2989 genpd_unlock(genpd);
2993 static int perf_state_show(struct seq_file *s, void *data)
2995 struct generic_pm_domain *genpd = s->private;
2997 if (genpd_lock_interruptible(genpd))
2998 return -ERESTARTSYS;
3000 seq_printf(s, "%u\n", genpd->performance_state);
3002 genpd_unlock(genpd);
3006 DEFINE_SHOW_ATTRIBUTE(summary);
3007 DEFINE_SHOW_ATTRIBUTE(status);
3008 DEFINE_SHOW_ATTRIBUTE(sub_domains);
3009 DEFINE_SHOW_ATTRIBUTE(idle_states);
3010 DEFINE_SHOW_ATTRIBUTE(active_time);
3011 DEFINE_SHOW_ATTRIBUTE(total_idle_time);
3012 DEFINE_SHOW_ATTRIBUTE(devices);
3013 DEFINE_SHOW_ATTRIBUTE(perf_state);
3015 static int __init genpd_debug_init(void)
3018 struct generic_pm_domain *genpd;
3020 genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
3022 debugfs_create_file("pm_genpd_summary", S_IRUGO, genpd_debugfs_dir,
3023 NULL, &summary_fops);
3025 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
3026 d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
3028 debugfs_create_file("current_state", 0444,
3029 d, genpd, &status_fops);
3030 debugfs_create_file("sub_domains", 0444,
3031 d, genpd, &sub_domains_fops);
3032 debugfs_create_file("idle_states", 0444,
3033 d, genpd, &idle_states_fops);
3034 debugfs_create_file("active_time", 0444,
3035 d, genpd, &active_time_fops);
3036 debugfs_create_file("total_idle_time", 0444,
3037 d, genpd, &total_idle_time_fops);
3038 debugfs_create_file("devices", 0444,
3039 d, genpd, &devices_fops);
3040 if (genpd->set_performance_state)
3041 debugfs_create_file("perf_state", 0444,
3042 d, genpd, &perf_state_fops);
3047 late_initcall(genpd_debug_init);
3049 static void __exit genpd_debug_exit(void)
3051 debugfs_remove_recursive(genpd_debugfs_dir);
3053 __exitcall(genpd_debug_exit);
3054 #endif /* CONFIG_DEBUG_FS */