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>
24 #include <linux/debugfs.h>
28 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
30 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
32 type (*__routine)(struct device *__d); \
33 type __ret = (type)0; \
35 __routine = genpd->dev_ops.callback; \
37 __ret = __routine(dev); \
42 static LIST_HEAD(gpd_list);
43 static DEFINE_MUTEX(gpd_list_lock);
45 struct genpd_lock_ops {
46 void (*lock)(struct generic_pm_domain *genpd);
47 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
48 int (*lock_interruptible)(struct generic_pm_domain *genpd);
49 void (*unlock)(struct generic_pm_domain *genpd);
52 static void genpd_lock_mtx(struct generic_pm_domain *genpd)
54 mutex_lock(&genpd->mlock);
57 static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
60 mutex_lock_nested(&genpd->mlock, depth);
63 static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
65 return mutex_lock_interruptible(&genpd->mlock);
68 static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
70 return mutex_unlock(&genpd->mlock);
73 static const struct genpd_lock_ops genpd_mtx_ops = {
74 .lock = genpd_lock_mtx,
75 .lock_nested = genpd_lock_nested_mtx,
76 .lock_interruptible = genpd_lock_interruptible_mtx,
77 .unlock = genpd_unlock_mtx,
80 static void genpd_lock_spin(struct generic_pm_domain *genpd)
81 __acquires(&genpd->slock)
85 spin_lock_irqsave(&genpd->slock, flags);
86 genpd->lock_flags = flags;
89 static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
91 __acquires(&genpd->slock)
95 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
96 genpd->lock_flags = flags;
99 static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
100 __acquires(&genpd->slock)
104 spin_lock_irqsave(&genpd->slock, flags);
105 genpd->lock_flags = flags;
109 static void genpd_unlock_spin(struct generic_pm_domain *genpd)
110 __releases(&genpd->slock)
112 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
115 static const struct genpd_lock_ops genpd_spin_ops = {
116 .lock = genpd_lock_spin,
117 .lock_nested = genpd_lock_nested_spin,
118 .lock_interruptible = genpd_lock_interruptible_spin,
119 .unlock = genpd_unlock_spin,
122 #define genpd_lock(p) p->lock_ops->lock(p)
123 #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
124 #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
125 #define genpd_unlock(p) p->lock_ops->unlock(p)
127 #define genpd_status_on(genpd) (genpd->status == GENPD_STATE_ON)
128 #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
129 #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
130 #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
131 #define genpd_is_cpu_domain(genpd) (genpd->flags & GENPD_FLAG_CPU_DOMAIN)
132 #define genpd_is_rpm_always_on(genpd) (genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
134 static inline bool irq_safe_dev_in_sleep_domain(struct device *dev,
135 const struct generic_pm_domain *genpd)
139 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
142 * Warn once if an IRQ safe device is attached to a domain, which
143 * callbacks are allowed to sleep. This indicates a suboptimal
144 * configuration for PM, but it doesn't matter for an always on domain.
146 if (genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd))
150 dev_warn_once(dev, "PM domain %s will not be powered off\n",
156 static int genpd_runtime_suspend(struct device *dev);
159 * Get the generic PM domain for a particular struct device.
160 * This validates the struct device pointer, the PM domain pointer,
161 * and checks that the PM domain pointer is a real generic PM domain.
162 * Any failure results in NULL being returned.
164 static struct generic_pm_domain *dev_to_genpd_safe(struct device *dev)
166 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
169 /* A genpd's always have its ->runtime_suspend() callback assigned. */
170 if (dev->pm_domain->ops.runtime_suspend == genpd_runtime_suspend)
171 return pd_to_genpd(dev->pm_domain);
177 * This should only be used where we are certain that the pm_domain
178 * attached to the device is a genpd domain.
180 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
182 if (IS_ERR_OR_NULL(dev->pm_domain))
183 return ERR_PTR(-EINVAL);
185 return pd_to_genpd(dev->pm_domain);
188 static int genpd_stop_dev(const struct generic_pm_domain *genpd,
191 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
194 static int genpd_start_dev(const struct generic_pm_domain *genpd,
197 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
200 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
204 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
205 ret = !!atomic_dec_and_test(&genpd->sd_count);
210 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
212 atomic_inc(&genpd->sd_count);
213 smp_mb__after_atomic();
216 #ifdef CONFIG_DEBUG_FS
217 static struct dentry *genpd_debugfs_dir;
219 static void genpd_debug_add(struct generic_pm_domain *genpd);
221 static void genpd_debug_remove(struct generic_pm_domain *genpd)
225 if (!genpd_debugfs_dir)
228 d = debugfs_lookup(genpd->name, genpd_debugfs_dir);
232 static void genpd_update_accounting(struct generic_pm_domain *genpd)
236 now = ktime_get_mono_fast_ns();
237 if (now <= genpd->accounting_time)
240 delta = now - genpd->accounting_time;
243 * If genpd->status is active, it means we are just
244 * out of off and so update the idle time and vice
247 if (genpd->status == GENPD_STATE_ON)
248 genpd->states[genpd->state_idx].idle_time += delta;
250 genpd->on_time += delta;
252 genpd->accounting_time = now;
255 static inline void genpd_debug_add(struct generic_pm_domain *genpd) {}
256 static inline void genpd_debug_remove(struct generic_pm_domain *genpd) {}
257 static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
260 static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd,
263 struct generic_pm_domain_data *pd_data;
264 struct pm_domain_data *pdd;
265 struct gpd_link *link;
267 /* New requested state is same as Max requested state */
268 if (state == genpd->performance_state)
271 /* New requested state is higher than Max requested state */
272 if (state > genpd->performance_state)
275 /* Traverse all devices within the domain */
276 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
277 pd_data = to_gpd_data(pdd);
279 if (pd_data->performance_state > state)
280 state = pd_data->performance_state;
284 * Traverse all sub-domains within the domain. This can be
285 * done without any additional locking as the link->performance_state
286 * field is protected by the parent genpd->lock, which is already taken.
288 * Also note that link->performance_state (subdomain's performance state
289 * requirement to parent domain) is different from
290 * link->child->performance_state (current performance state requirement
291 * of the devices/sub-domains of the subdomain) and so can have a
294 * Note that we also take vote from powered-off sub-domains into account
295 * as the same is done for devices right now.
297 list_for_each_entry(link, &genpd->parent_links, parent_node) {
298 if (link->performance_state > state)
299 state = link->performance_state;
305 static int genpd_xlate_performance_state(struct generic_pm_domain *genpd,
306 struct generic_pm_domain *parent,
309 if (!parent->set_performance_state)
312 return dev_pm_opp_xlate_performance_state(genpd->opp_table,
317 static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
318 unsigned int state, int depth)
320 struct generic_pm_domain *parent;
321 struct gpd_link *link;
322 int parent_state, ret;
324 if (state == genpd->performance_state)
327 /* Propagate to parents of genpd */
328 list_for_each_entry(link, &genpd->child_links, child_node) {
329 parent = link->parent;
331 /* Find parent's performance state */
332 ret = genpd_xlate_performance_state(genpd, parent, state);
333 if (unlikely(ret < 0))
338 genpd_lock_nested(parent, depth + 1);
340 link->prev_performance_state = link->performance_state;
341 link->performance_state = parent_state;
342 parent_state = _genpd_reeval_performance_state(parent,
344 ret = _genpd_set_performance_state(parent, parent_state, depth + 1);
346 link->performance_state = link->prev_performance_state;
348 genpd_unlock(parent);
354 if (genpd->set_performance_state) {
355 ret = genpd->set_performance_state(genpd, state);
360 genpd->performance_state = state;
364 /* Encountered an error, lets rollback */
365 list_for_each_entry_continue_reverse(link, &genpd->child_links,
367 parent = link->parent;
369 genpd_lock_nested(parent, depth + 1);
371 parent_state = link->prev_performance_state;
372 link->performance_state = parent_state;
374 parent_state = _genpd_reeval_performance_state(parent,
376 if (_genpd_set_performance_state(parent, parent_state, depth + 1)) {
377 pr_err("%s: Failed to roll back to %d performance state\n",
378 parent->name, parent_state);
381 genpd_unlock(parent);
387 static int genpd_set_performance_state(struct device *dev, unsigned int state)
389 struct generic_pm_domain *genpd = dev_to_genpd(dev);
390 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
391 unsigned int prev_state;
394 prev_state = gpd_data->performance_state;
395 if (prev_state == state)
398 gpd_data->performance_state = state;
399 state = _genpd_reeval_performance_state(genpd, state);
401 ret = _genpd_set_performance_state(genpd, state, 0);
403 gpd_data->performance_state = prev_state;
408 static int genpd_drop_performance_state(struct device *dev)
410 unsigned int prev_state = dev_gpd_data(dev)->performance_state;
412 if (!genpd_set_performance_state(dev, 0))
418 static void genpd_restore_performance_state(struct device *dev,
422 genpd_set_performance_state(dev, state);
426 * dev_pm_genpd_set_performance_state- Set performance state of device's power
429 * @dev: Device for which the performance-state needs to be set.
430 * @state: Target performance state of the device. This can be set as 0 when the
431 * device doesn't have any performance state constraints left (And so
432 * the device wouldn't participate anymore to find the target
433 * performance state of the genpd).
435 * It is assumed that the users guarantee that the genpd wouldn't be detached
436 * while this routine is getting called.
438 * Returns 0 on success and negative error values on failures.
440 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
442 struct generic_pm_domain *genpd;
445 genpd = dev_to_genpd_safe(dev);
449 if (WARN_ON(!dev->power.subsys_data ||
450 !dev->power.subsys_data->domain_data))
454 if (pm_runtime_suspended(dev)) {
455 dev_gpd_data(dev)->rpm_pstate = state;
457 ret = genpd_set_performance_state(dev, state);
459 dev_gpd_data(dev)->rpm_pstate = 0;
465 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
468 * dev_pm_genpd_set_next_wakeup - Notify PM framework of an impending wakeup.
470 * @dev: Device to handle
471 * @next: impending interrupt/wakeup for the device
474 * Allow devices to inform of the next wakeup. It's assumed that the users
475 * guarantee that the genpd wouldn't be detached while this routine is getting
476 * called. Additionally, it's also assumed that @dev isn't runtime suspended
478 * Although devices are expected to update the next_wakeup after the end of
479 * their usecase as well, it is possible the devices themselves may not know
480 * about that, so stale @next will be ignored when powering off the domain.
482 void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
484 struct generic_pm_domain *genpd;
485 struct gpd_timing_data *td;
487 genpd = dev_to_genpd_safe(dev);
491 td = to_gpd_data(dev->power.subsys_data->domain_data)->td;
493 td->next_wakeup = next;
495 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_next_wakeup);
497 static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
499 unsigned int state_idx = genpd->state_idx;
504 /* Notify consumers that we are about to power on. */
505 ret = raw_notifier_call_chain_robust(&genpd->power_notifiers,
507 GENPD_NOTIFY_OFF, NULL);
508 ret = notifier_to_errno(ret);
512 if (!genpd->power_on)
515 timed = timed && genpd->gd && !genpd->states[state_idx].fwnode;
517 ret = genpd->power_on(genpd);
524 time_start = ktime_get();
525 ret = genpd->power_on(genpd);
529 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
530 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
533 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
534 genpd->gd->max_off_time_changed = true;
535 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
536 genpd->name, "on", elapsed_ns);
539 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_ON, NULL);
542 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_OFF,
547 static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
549 unsigned int state_idx = genpd->state_idx;
554 /* Notify consumers that we are about to power off. */
555 ret = raw_notifier_call_chain_robust(&genpd->power_notifiers,
556 GENPD_NOTIFY_PRE_OFF,
557 GENPD_NOTIFY_ON, NULL);
558 ret = notifier_to_errno(ret);
562 if (!genpd->power_off)
565 timed = timed && genpd->gd && !genpd->states[state_idx].fwnode;
567 ret = genpd->power_off(genpd);
574 time_start = ktime_get();
575 ret = genpd->power_off(genpd);
579 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
580 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
583 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
584 genpd->gd->max_off_time_changed = true;
585 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
586 genpd->name, "off", elapsed_ns);
589 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_OFF,
593 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_ON, NULL);
598 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
599 * @genpd: PM domain to power off.
601 * Queue up the execution of genpd_power_off() unless it's already been done
604 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
606 queue_work(pm_wq, &genpd->power_off_work);
610 * genpd_power_off - Remove power from a given PM domain.
611 * @genpd: PM domain to power down.
612 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
613 * RPM status of the releated device is in an intermediate state, not yet turned
614 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
615 * be RPM_SUSPENDED, while it tries to power off the PM domain.
616 * @depth: nesting count for lockdep.
618 * If all of the @genpd's devices have been suspended and all of its subdomains
619 * have been powered down, remove power from @genpd.
621 static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
624 struct pm_domain_data *pdd;
625 struct gpd_link *link;
626 unsigned int not_suspended = 0;
630 * Do not try to power off the domain in the following situations:
631 * (1) The domain is already in the "power off" state.
632 * (2) System suspend is in progress.
634 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
638 * Abort power off for the PM domain in the following situations:
639 * (1) The domain is configured as always on.
640 * (2) When the domain has a subdomain being powered on.
642 if (genpd_is_always_on(genpd) ||
643 genpd_is_rpm_always_on(genpd) ||
644 atomic_read(&genpd->sd_count) > 0)
648 * The children must be in their deepest (powered-off) states to allow
649 * the parent to be powered off. Note that, there's no need for
650 * additional locking, as powering on a child, requires the parent's
651 * lock to be acquired first.
653 list_for_each_entry(link, &genpd->parent_links, parent_node) {
654 struct generic_pm_domain *child = link->child;
655 if (child->state_idx < child->state_count - 1)
659 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
661 * Do not allow PM domain to be powered off, when an IRQ safe
662 * device is part of a non-IRQ safe domain.
664 if (!pm_runtime_suspended(pdd->dev) ||
665 irq_safe_dev_in_sleep_domain(pdd->dev, genpd))
669 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
672 if (genpd->gov && genpd->gov->power_down_ok) {
673 if (!genpd->gov->power_down_ok(&genpd->domain))
677 /* Default to shallowest state. */
679 genpd->state_idx = 0;
681 /* Don't power off, if a child domain is waiting to power on. */
682 if (atomic_read(&genpd->sd_count) > 0)
685 ret = _genpd_power_off(genpd, true);
687 genpd->states[genpd->state_idx].rejected++;
691 genpd->status = GENPD_STATE_OFF;
692 genpd_update_accounting(genpd);
693 genpd->states[genpd->state_idx].usage++;
695 list_for_each_entry(link, &genpd->child_links, child_node) {
696 genpd_sd_counter_dec(link->parent);
697 genpd_lock_nested(link->parent, depth + 1);
698 genpd_power_off(link->parent, false, depth + 1);
699 genpd_unlock(link->parent);
706 * genpd_power_on - Restore power to a given PM domain and its parents.
707 * @genpd: PM domain to power up.
708 * @depth: nesting count for lockdep.
710 * Restore power to @genpd and all of its parents so that it is possible to
711 * resume a device belonging to it.
713 static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
715 struct gpd_link *link;
718 if (genpd_status_on(genpd))
722 * The list is guaranteed not to change while the loop below is being
723 * executed, unless one of the parents' .power_on() callbacks fiddles
726 list_for_each_entry(link, &genpd->child_links, child_node) {
727 struct generic_pm_domain *parent = link->parent;
729 genpd_sd_counter_inc(parent);
731 genpd_lock_nested(parent, depth + 1);
732 ret = genpd_power_on(parent, depth + 1);
733 genpd_unlock(parent);
736 genpd_sd_counter_dec(parent);
741 ret = _genpd_power_on(genpd, true);
745 genpd->status = GENPD_STATE_ON;
746 genpd_update_accounting(genpd);
751 list_for_each_entry_continue_reverse(link,
754 genpd_sd_counter_dec(link->parent);
755 genpd_lock_nested(link->parent, depth + 1);
756 genpd_power_off(link->parent, false, depth + 1);
757 genpd_unlock(link->parent);
763 static int genpd_dev_pm_start(struct device *dev)
765 struct generic_pm_domain *genpd = dev_to_genpd(dev);
767 return genpd_start_dev(genpd, dev);
770 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
771 unsigned long val, void *ptr)
773 struct generic_pm_domain_data *gpd_data;
776 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
777 dev = gpd_data->base.dev;
780 struct generic_pm_domain *genpd = ERR_PTR(-ENODATA);
781 struct pm_domain_data *pdd;
782 struct gpd_timing_data *td;
784 spin_lock_irq(&dev->power.lock);
786 pdd = dev->power.subsys_data ?
787 dev->power.subsys_data->domain_data : NULL;
789 td = to_gpd_data(pdd)->td;
791 td->constraint_changed = true;
792 genpd = dev_to_genpd(dev);
796 spin_unlock_irq(&dev->power.lock);
798 if (!IS_ERR(genpd)) {
800 genpd->gd->max_off_time_changed = true;
805 if (!dev || dev->power.ignore_children)
813 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
814 * @work: Work structure used for scheduling the execution of this function.
816 static void genpd_power_off_work_fn(struct work_struct *work)
818 struct generic_pm_domain *genpd;
820 genpd = container_of(work, struct generic_pm_domain, power_off_work);
823 genpd_power_off(genpd, false, 0);
828 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
829 * @dev: Device to handle.
831 static int __genpd_runtime_suspend(struct device *dev)
833 int (*cb)(struct device *__dev);
835 if (dev->type && dev->type->pm)
836 cb = dev->type->pm->runtime_suspend;
837 else if (dev->class && dev->class->pm)
838 cb = dev->class->pm->runtime_suspend;
839 else if (dev->bus && dev->bus->pm)
840 cb = dev->bus->pm->runtime_suspend;
844 if (!cb && dev->driver && dev->driver->pm)
845 cb = dev->driver->pm->runtime_suspend;
847 return cb ? cb(dev) : 0;
851 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
852 * @dev: Device to handle.
854 static int __genpd_runtime_resume(struct device *dev)
856 int (*cb)(struct device *__dev);
858 if (dev->type && dev->type->pm)
859 cb = dev->type->pm->runtime_resume;
860 else if (dev->class && dev->class->pm)
861 cb = dev->class->pm->runtime_resume;
862 else if (dev->bus && dev->bus->pm)
863 cb = dev->bus->pm->runtime_resume;
867 if (!cb && dev->driver && dev->driver->pm)
868 cb = dev->driver->pm->runtime_resume;
870 return cb ? cb(dev) : 0;
874 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
875 * @dev: Device to suspend.
877 * Carry out a runtime suspend of a device under the assumption that its
878 * pm_domain field points to the domain member of an object of type
879 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
881 static int genpd_runtime_suspend(struct device *dev)
883 struct generic_pm_domain *genpd;
884 bool (*suspend_ok)(struct device *__dev);
885 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
886 struct gpd_timing_data *td = gpd_data->td;
887 bool runtime_pm = pm_runtime_enabled(dev);
888 ktime_t time_start = 0;
892 dev_dbg(dev, "%s()\n", __func__);
894 genpd = dev_to_genpd(dev);
899 * A runtime PM centric subsystem/driver may re-use the runtime PM
900 * callbacks for other purposes than runtime PM. In those scenarios
901 * runtime PM is disabled. Under these circumstances, we shall skip
902 * validating/measuring the PM QoS latency.
904 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
905 if (runtime_pm && suspend_ok && !suspend_ok(dev))
908 /* Measure suspend latency. */
909 if (td && runtime_pm)
910 time_start = ktime_get();
912 ret = __genpd_runtime_suspend(dev);
916 ret = genpd_stop_dev(genpd, dev);
918 __genpd_runtime_resume(dev);
922 /* Update suspend latency value if the measured time exceeds it. */
923 if (td && runtime_pm) {
924 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
925 if (elapsed_ns > td->suspend_latency_ns) {
926 td->suspend_latency_ns = elapsed_ns;
927 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
929 genpd->gd->max_off_time_changed = true;
930 td->constraint_changed = true;
935 * If power.irq_safe is set, this routine may be run with
936 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
938 if (irq_safe_dev_in_sleep_domain(dev, genpd))
942 gpd_data->rpm_pstate = genpd_drop_performance_state(dev);
943 genpd_power_off(genpd, true, 0);
950 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
951 * @dev: Device to resume.
953 * Carry out a runtime resume of a device under the assumption that its
954 * pm_domain field points to the domain member of an object of type
955 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
957 static int genpd_runtime_resume(struct device *dev)
959 struct generic_pm_domain *genpd;
960 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
961 struct gpd_timing_data *td = gpd_data->td;
962 bool timed = td && pm_runtime_enabled(dev);
963 ktime_t time_start = 0;
967 dev_dbg(dev, "%s()\n", __func__);
969 genpd = dev_to_genpd(dev);
974 * As we don't power off a non IRQ safe domain, which holds
975 * an IRQ safe device, we don't need to restore power to it.
977 if (irq_safe_dev_in_sleep_domain(dev, genpd))
981 ret = genpd_power_on(genpd, 0);
983 genpd_restore_performance_state(dev, gpd_data->rpm_pstate);
990 /* Measure resume latency. */
992 time_start = ktime_get();
994 ret = genpd_start_dev(genpd, dev);
998 ret = __genpd_runtime_resume(dev);
1002 /* Update resume latency value if the measured time exceeds it. */
1004 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
1005 if (elapsed_ns > td->resume_latency_ns) {
1006 td->resume_latency_ns = elapsed_ns;
1007 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
1009 genpd->gd->max_off_time_changed = true;
1010 td->constraint_changed = true;
1017 genpd_stop_dev(genpd, dev);
1019 if (!pm_runtime_is_irq_safe(dev) || genpd_is_irq_safe(genpd)) {
1021 gpd_data->rpm_pstate = genpd_drop_performance_state(dev);
1022 genpd_power_off(genpd, true, 0);
1023 genpd_unlock(genpd);
1029 static bool pd_ignore_unused;
1030 static int __init pd_ignore_unused_setup(char *__unused)
1032 pd_ignore_unused = true;
1035 __setup("pd_ignore_unused", pd_ignore_unused_setup);
1038 * genpd_power_off_unused - Power off all PM domains with no devices in use.
1040 static int __init genpd_power_off_unused(void)
1042 struct generic_pm_domain *genpd;
1044 if (pd_ignore_unused) {
1045 pr_warn("genpd: Not disabling unused power domains\n");
1049 mutex_lock(&gpd_list_lock);
1051 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
1052 genpd_queue_power_off_work(genpd);
1054 mutex_unlock(&gpd_list_lock);
1058 late_initcall(genpd_power_off_unused);
1060 #ifdef CONFIG_PM_SLEEP
1063 * genpd_sync_power_off - Synchronously power off a PM domain and its parents.
1064 * @genpd: PM domain to power off, if possible.
1065 * @use_lock: use the lock.
1066 * @depth: nesting count for lockdep.
1068 * Check if the given PM domain can be powered off (during system suspend or
1069 * hibernation) and do that if so. Also, in that case propagate to its parents.
1071 * This function is only called in "noirq" and "syscore" stages of system power
1072 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
1073 * these cases the lock must be held.
1075 static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
1078 struct gpd_link *link;
1080 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
1083 if (genpd->suspended_count != genpd->device_count
1084 || atomic_read(&genpd->sd_count) > 0)
1087 /* Check that the children are in their deepest (powered-off) state. */
1088 list_for_each_entry(link, &genpd->parent_links, parent_node) {
1089 struct generic_pm_domain *child = link->child;
1090 if (child->state_idx < child->state_count - 1)
1094 /* Choose the deepest state when suspending */
1095 genpd->state_idx = genpd->state_count - 1;
1096 if (_genpd_power_off(genpd, false))
1099 genpd->status = GENPD_STATE_OFF;
1101 list_for_each_entry(link, &genpd->child_links, child_node) {
1102 genpd_sd_counter_dec(link->parent);
1105 genpd_lock_nested(link->parent, depth + 1);
1107 genpd_sync_power_off(link->parent, use_lock, depth + 1);
1110 genpd_unlock(link->parent);
1115 * genpd_sync_power_on - Synchronously power on a PM domain and its parents.
1116 * @genpd: PM domain to power on.
1117 * @use_lock: use the lock.
1118 * @depth: nesting count for lockdep.
1120 * This function is only called in "noirq" and "syscore" stages of system power
1121 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
1122 * these cases the lock must be held.
1124 static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
1127 struct gpd_link *link;
1129 if (genpd_status_on(genpd))
1132 list_for_each_entry(link, &genpd->child_links, child_node) {
1133 genpd_sd_counter_inc(link->parent);
1136 genpd_lock_nested(link->parent, depth + 1);
1138 genpd_sync_power_on(link->parent, use_lock, depth + 1);
1141 genpd_unlock(link->parent);
1144 _genpd_power_on(genpd, false);
1145 genpd->status = GENPD_STATE_ON;
1149 * genpd_prepare - Start power transition of a device in a PM domain.
1150 * @dev: Device to start the transition of.
1152 * Start a power transition of a device (during a system-wide power transition)
1153 * under the assumption that its pm_domain field points to the domain member of
1154 * an object of type struct generic_pm_domain representing a PM domain
1155 * consisting of I/O devices.
1157 static int genpd_prepare(struct device *dev)
1159 struct generic_pm_domain *genpd;
1162 dev_dbg(dev, "%s()\n", __func__);
1164 genpd = dev_to_genpd(dev);
1170 if (genpd->prepared_count++ == 0)
1171 genpd->suspended_count = 0;
1173 genpd_unlock(genpd);
1175 ret = pm_generic_prepare(dev);
1179 genpd->prepared_count--;
1181 genpd_unlock(genpd);
1184 /* Never return 1, as genpd don't cope with the direct_complete path. */
1185 return ret >= 0 ? 0 : ret;
1189 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1191 * @dev: Device to suspend.
1192 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
1194 * Stop the device and remove power from the domain if all devices in it have
1197 static int genpd_finish_suspend(struct device *dev, bool poweroff)
1199 struct generic_pm_domain *genpd;
1202 genpd = dev_to_genpd(dev);
1207 ret = pm_generic_poweroff_noirq(dev);
1209 ret = pm_generic_suspend_noirq(dev);
1213 if (device_wakeup_path(dev) && genpd_is_active_wakeup(genpd))
1216 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1217 !pm_runtime_status_suspended(dev)) {
1218 ret = genpd_stop_dev(genpd, dev);
1221 pm_generic_restore_noirq(dev);
1223 pm_generic_resume_noirq(dev);
1229 genpd->suspended_count++;
1230 genpd_sync_power_off(genpd, true, 0);
1231 genpd_unlock(genpd);
1237 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1238 * @dev: Device to suspend.
1240 * Stop the device and remove power from the domain if all devices in it have
1243 static int genpd_suspend_noirq(struct device *dev)
1245 dev_dbg(dev, "%s()\n", __func__);
1247 return genpd_finish_suspend(dev, false);
1251 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1252 * @dev: Device to resume.
1254 * Restore power to the device's PM domain, if necessary, and start the device.
1256 static int genpd_resume_noirq(struct device *dev)
1258 struct generic_pm_domain *genpd;
1261 dev_dbg(dev, "%s()\n", __func__);
1263 genpd = dev_to_genpd(dev);
1267 if (device_wakeup_path(dev) && genpd_is_active_wakeup(genpd))
1268 return pm_generic_resume_noirq(dev);
1271 genpd_sync_power_on(genpd, true, 0);
1272 genpd->suspended_count--;
1273 genpd_unlock(genpd);
1275 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1276 !pm_runtime_status_suspended(dev)) {
1277 ret = genpd_start_dev(genpd, dev);
1282 return pm_generic_resume_noirq(dev);
1286 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1287 * @dev: Device to freeze.
1289 * Carry out a late freeze of a device under the assumption that its
1290 * pm_domain field points to the domain member of an object of type
1291 * struct generic_pm_domain representing a power domain consisting of I/O
1294 static int genpd_freeze_noirq(struct device *dev)
1296 const struct generic_pm_domain *genpd;
1299 dev_dbg(dev, "%s()\n", __func__);
1301 genpd = dev_to_genpd(dev);
1305 ret = pm_generic_freeze_noirq(dev);
1309 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1310 !pm_runtime_status_suspended(dev))
1311 ret = genpd_stop_dev(genpd, dev);
1317 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1318 * @dev: Device to thaw.
1320 * Start the device, unless power has been removed from the domain already
1321 * before the system transition.
1323 static int genpd_thaw_noirq(struct device *dev)
1325 const struct generic_pm_domain *genpd;
1328 dev_dbg(dev, "%s()\n", __func__);
1330 genpd = dev_to_genpd(dev);
1334 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1335 !pm_runtime_status_suspended(dev)) {
1336 ret = genpd_start_dev(genpd, dev);
1341 return pm_generic_thaw_noirq(dev);
1345 * genpd_poweroff_noirq - Completion of hibernation of device in an
1347 * @dev: Device to poweroff.
1349 * Stop the device and remove power from the domain if all devices in it have
1352 static int genpd_poweroff_noirq(struct device *dev)
1354 dev_dbg(dev, "%s()\n", __func__);
1356 return genpd_finish_suspend(dev, true);
1360 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1361 * @dev: Device to resume.
1363 * Make sure the domain will be in the same power state as before the
1364 * hibernation the system is resuming from and start the device if necessary.
1366 static int genpd_restore_noirq(struct device *dev)
1368 struct generic_pm_domain *genpd;
1371 dev_dbg(dev, "%s()\n", __func__);
1373 genpd = dev_to_genpd(dev);
1378 * At this point suspended_count == 0 means we are being run for the
1379 * first time for the given domain in the present cycle.
1382 if (genpd->suspended_count++ == 0) {
1384 * The boot kernel might put the domain into arbitrary state,
1385 * so make it appear as powered off to genpd_sync_power_on(),
1386 * so that it tries to power it on in case it was really off.
1388 genpd->status = GENPD_STATE_OFF;
1391 genpd_sync_power_on(genpd, true, 0);
1392 genpd_unlock(genpd);
1394 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1395 !pm_runtime_status_suspended(dev)) {
1396 ret = genpd_start_dev(genpd, dev);
1401 return pm_generic_restore_noirq(dev);
1405 * genpd_complete - Complete power transition of a device in a power domain.
1406 * @dev: Device to complete the transition of.
1408 * Complete a power transition of a device (during a system-wide power
1409 * transition) under the assumption that its pm_domain field points to the
1410 * domain member of an object of type struct generic_pm_domain representing
1411 * a power domain consisting of I/O devices.
1413 static void genpd_complete(struct device *dev)
1415 struct generic_pm_domain *genpd;
1417 dev_dbg(dev, "%s()\n", __func__);
1419 genpd = dev_to_genpd(dev);
1423 pm_generic_complete(dev);
1427 genpd->prepared_count--;
1428 if (!genpd->prepared_count)
1429 genpd_queue_power_off_work(genpd);
1431 genpd_unlock(genpd);
1434 static void genpd_switch_state(struct device *dev, bool suspend)
1436 struct generic_pm_domain *genpd;
1439 genpd = dev_to_genpd_safe(dev);
1443 use_lock = genpd_is_irq_safe(genpd);
1449 genpd->suspended_count++;
1450 genpd_sync_power_off(genpd, use_lock, 0);
1452 genpd_sync_power_on(genpd, use_lock, 0);
1453 genpd->suspended_count--;
1457 genpd_unlock(genpd);
1461 * dev_pm_genpd_suspend - Synchronously try to suspend the genpd for @dev
1462 * @dev: The device that is attached to the genpd, that can be suspended.
1464 * This routine should typically be called for a device that needs to be
1465 * suspended during the syscore suspend phase. It may also be called during
1466 * suspend-to-idle to suspend a corresponding CPU device that is attached to a
1469 void dev_pm_genpd_suspend(struct device *dev)
1471 genpd_switch_state(dev, true);
1473 EXPORT_SYMBOL_GPL(dev_pm_genpd_suspend);
1476 * dev_pm_genpd_resume - Synchronously try to resume the genpd for @dev
1477 * @dev: The device that is attached to the genpd, which needs to be resumed.
1479 * This routine should typically be called for a device that needs to be resumed
1480 * during the syscore resume phase. It may also be called during suspend-to-idle
1481 * to resume a corresponding CPU device that is attached to a genpd.
1483 void dev_pm_genpd_resume(struct device *dev)
1485 genpd_switch_state(dev, false);
1487 EXPORT_SYMBOL_GPL(dev_pm_genpd_resume);
1489 #else /* !CONFIG_PM_SLEEP */
1491 #define genpd_prepare NULL
1492 #define genpd_suspend_noirq NULL
1493 #define genpd_resume_noirq NULL
1494 #define genpd_freeze_noirq NULL
1495 #define genpd_thaw_noirq NULL
1496 #define genpd_poweroff_noirq NULL
1497 #define genpd_restore_noirq NULL
1498 #define genpd_complete NULL
1500 #endif /* CONFIG_PM_SLEEP */
1502 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1505 struct generic_pm_domain_data *gpd_data;
1506 struct gpd_timing_data *td;
1509 ret = dev_pm_get_subsys_data(dev);
1511 return ERR_PTR(ret);
1513 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1519 gpd_data->base.dev = dev;
1520 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1522 /* Allocate data used by a governor. */
1524 td = kzalloc(sizeof(*td), GFP_KERNEL);
1530 td->constraint_changed = true;
1531 td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
1532 td->next_wakeup = KTIME_MAX;
1536 spin_lock_irq(&dev->power.lock);
1538 if (dev->power.subsys_data->domain_data)
1541 dev->power.subsys_data->domain_data = &gpd_data->base;
1543 spin_unlock_irq(&dev->power.lock);
1551 kfree(gpd_data->td);
1554 dev_pm_put_subsys_data(dev);
1555 return ERR_PTR(ret);
1558 static void genpd_free_dev_data(struct device *dev,
1559 struct generic_pm_domain_data *gpd_data)
1561 spin_lock_irq(&dev->power.lock);
1563 dev->power.subsys_data->domain_data = NULL;
1565 spin_unlock_irq(&dev->power.lock);
1567 kfree(gpd_data->td);
1569 dev_pm_put_subsys_data(dev);
1572 static void genpd_update_cpumask(struct generic_pm_domain *genpd,
1573 int cpu, bool set, unsigned int depth)
1575 struct gpd_link *link;
1577 if (!genpd_is_cpu_domain(genpd))
1580 list_for_each_entry(link, &genpd->child_links, child_node) {
1581 struct generic_pm_domain *parent = link->parent;
1583 genpd_lock_nested(parent, depth + 1);
1584 genpd_update_cpumask(parent, cpu, set, depth + 1);
1585 genpd_unlock(parent);
1589 cpumask_set_cpu(cpu, genpd->cpus);
1591 cpumask_clear_cpu(cpu, genpd->cpus);
1594 static void genpd_set_cpumask(struct generic_pm_domain *genpd, int cpu)
1597 genpd_update_cpumask(genpd, cpu, true, 0);
1600 static void genpd_clear_cpumask(struct generic_pm_domain *genpd, int cpu)
1603 genpd_update_cpumask(genpd, cpu, false, 0);
1606 static int genpd_get_cpu(struct generic_pm_domain *genpd, struct device *dev)
1610 if (!genpd_is_cpu_domain(genpd))
1613 for_each_possible_cpu(cpu) {
1614 if (get_cpu_device(cpu) == dev)
1621 static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1622 struct device *base_dev)
1624 struct genpd_governor_data *gd = genpd->gd;
1625 struct generic_pm_domain_data *gpd_data;
1628 dev_dbg(dev, "%s()\n", __func__);
1630 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1633 gpd_data = genpd_alloc_dev_data(dev, gd);
1634 if (IS_ERR(gpd_data))
1635 return PTR_ERR(gpd_data);
1637 gpd_data->cpu = genpd_get_cpu(genpd, base_dev);
1639 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1645 genpd_set_cpumask(genpd, gpd_data->cpu);
1646 dev_pm_domain_set(dev, &genpd->domain);
1648 genpd->device_count++;
1650 gd->max_off_time_changed = true;
1652 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1654 genpd_unlock(genpd);
1657 genpd_free_dev_data(dev, gpd_data);
1659 dev_pm_qos_add_notifier(dev, &gpd_data->nb,
1660 DEV_PM_QOS_RESUME_LATENCY);
1666 * pm_genpd_add_device - Add a device to an I/O PM domain.
1667 * @genpd: PM domain to add the device to.
1668 * @dev: Device to be added.
1670 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1674 mutex_lock(&gpd_list_lock);
1675 ret = genpd_add_device(genpd, dev, dev);
1676 mutex_unlock(&gpd_list_lock);
1680 EXPORT_SYMBOL_GPL(pm_genpd_add_device);
1682 static int genpd_remove_device(struct generic_pm_domain *genpd,
1685 struct generic_pm_domain_data *gpd_data;
1686 struct pm_domain_data *pdd;
1689 dev_dbg(dev, "%s()\n", __func__);
1691 pdd = dev->power.subsys_data->domain_data;
1692 gpd_data = to_gpd_data(pdd);
1693 dev_pm_qos_remove_notifier(dev, &gpd_data->nb,
1694 DEV_PM_QOS_RESUME_LATENCY);
1698 if (genpd->prepared_count > 0) {
1703 genpd->device_count--;
1705 genpd->gd->max_off_time_changed = true;
1707 genpd_clear_cpumask(genpd, gpd_data->cpu);
1708 dev_pm_domain_set(dev, NULL);
1710 list_del_init(&pdd->list_node);
1712 genpd_unlock(genpd);
1714 if (genpd->detach_dev)
1715 genpd->detach_dev(genpd, dev);
1717 genpd_free_dev_data(dev, gpd_data);
1722 genpd_unlock(genpd);
1723 dev_pm_qos_add_notifier(dev, &gpd_data->nb, DEV_PM_QOS_RESUME_LATENCY);
1729 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1730 * @dev: Device to be removed.
1732 int pm_genpd_remove_device(struct device *dev)
1734 struct generic_pm_domain *genpd = dev_to_genpd_safe(dev);
1739 return genpd_remove_device(genpd, dev);
1741 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1744 * dev_pm_genpd_add_notifier - Add a genpd power on/off notifier for @dev
1746 * @dev: Device that should be associated with the notifier
1747 * @nb: The notifier block to register
1749 * Users may call this function to add a genpd power on/off notifier for an
1750 * attached @dev. Only one notifier per device is allowed. The notifier is
1751 * sent when genpd is powering on/off the PM domain.
1753 * It is assumed that the user guarantee that the genpd wouldn't be detached
1754 * while this routine is getting called.
1756 * Returns 0 on success and negative error values on failures.
1758 int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb)
1760 struct generic_pm_domain *genpd;
1761 struct generic_pm_domain_data *gpd_data;
1764 genpd = dev_to_genpd_safe(dev);
1768 if (WARN_ON(!dev->power.subsys_data ||
1769 !dev->power.subsys_data->domain_data))
1772 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1773 if (gpd_data->power_nb)
1777 ret = raw_notifier_chain_register(&genpd->power_notifiers, nb);
1778 genpd_unlock(genpd);
1781 dev_warn(dev, "failed to add notifier for PM domain %s\n",
1786 gpd_data->power_nb = nb;
1789 EXPORT_SYMBOL_GPL(dev_pm_genpd_add_notifier);
1792 * dev_pm_genpd_remove_notifier - Remove a genpd power on/off notifier for @dev
1794 * @dev: Device that is associated with the notifier
1796 * Users may call this function to remove a genpd power on/off notifier for an
1799 * It is assumed that the user guarantee that the genpd wouldn't be detached
1800 * while this routine is getting called.
1802 * Returns 0 on success and negative error values on failures.
1804 int dev_pm_genpd_remove_notifier(struct device *dev)
1806 struct generic_pm_domain *genpd;
1807 struct generic_pm_domain_data *gpd_data;
1810 genpd = dev_to_genpd_safe(dev);
1814 if (WARN_ON(!dev->power.subsys_data ||
1815 !dev->power.subsys_data->domain_data))
1818 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1819 if (!gpd_data->power_nb)
1823 ret = raw_notifier_chain_unregister(&genpd->power_notifiers,
1824 gpd_data->power_nb);
1825 genpd_unlock(genpd);
1828 dev_warn(dev, "failed to remove notifier for PM domain %s\n",
1833 gpd_data->power_nb = NULL;
1836 EXPORT_SYMBOL_GPL(dev_pm_genpd_remove_notifier);
1838 static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1839 struct generic_pm_domain *subdomain)
1841 struct gpd_link *link, *itr;
1844 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1845 || genpd == subdomain)
1849 * If the domain can be powered on/off in an IRQ safe
1850 * context, ensure that the subdomain can also be
1851 * powered on/off in that context.
1853 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1854 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1855 genpd->name, subdomain->name);
1859 link = kzalloc(sizeof(*link), GFP_KERNEL);
1863 genpd_lock(subdomain);
1864 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1866 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1871 list_for_each_entry(itr, &genpd->parent_links, parent_node) {
1872 if (itr->child == subdomain && itr->parent == genpd) {
1878 link->parent = genpd;
1879 list_add_tail(&link->parent_node, &genpd->parent_links);
1880 link->child = subdomain;
1881 list_add_tail(&link->child_node, &subdomain->child_links);
1882 if (genpd_status_on(subdomain))
1883 genpd_sd_counter_inc(genpd);
1886 genpd_unlock(genpd);
1887 genpd_unlock(subdomain);
1894 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1895 * @genpd: Leader PM domain to add the subdomain to.
1896 * @subdomain: Subdomain to be added.
1898 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1899 struct generic_pm_domain *subdomain)
1903 mutex_lock(&gpd_list_lock);
1904 ret = genpd_add_subdomain(genpd, subdomain);
1905 mutex_unlock(&gpd_list_lock);
1909 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1912 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1913 * @genpd: Leader PM domain to remove the subdomain from.
1914 * @subdomain: Subdomain to be removed.
1916 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1917 struct generic_pm_domain *subdomain)
1919 struct gpd_link *l, *link;
1922 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1925 genpd_lock(subdomain);
1926 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1928 if (!list_empty(&subdomain->parent_links) || subdomain->device_count) {
1929 pr_warn("%s: unable to remove subdomain %s\n",
1930 genpd->name, subdomain->name);
1935 list_for_each_entry_safe(link, l, &genpd->parent_links, parent_node) {
1936 if (link->child != subdomain)
1939 list_del(&link->parent_node);
1940 list_del(&link->child_node);
1942 if (genpd_status_on(subdomain))
1943 genpd_sd_counter_dec(genpd);
1950 genpd_unlock(genpd);
1951 genpd_unlock(subdomain);
1955 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1957 static void genpd_free_default_power_state(struct genpd_power_state *states,
1958 unsigned int state_count)
1963 static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1965 struct genpd_power_state *state;
1967 state = kzalloc(sizeof(*state), GFP_KERNEL);
1971 genpd->states = state;
1972 genpd->state_count = 1;
1973 genpd->free_states = genpd_free_default_power_state;
1978 static int genpd_alloc_data(struct generic_pm_domain *genpd)
1980 struct genpd_governor_data *gd = NULL;
1983 if (genpd_is_cpu_domain(genpd) &&
1984 !zalloc_cpumask_var(&genpd->cpus, GFP_KERNEL))
1988 gd = kzalloc(sizeof(*gd), GFP_KERNEL);
1994 gd->max_off_time_ns = -1;
1995 gd->max_off_time_changed = true;
1996 gd->next_wakeup = KTIME_MAX;
1999 /* Use only one "off" state if there were no states declared */
2000 if (genpd->state_count == 0) {
2001 ret = genpd_set_default_power_state(genpd);
2010 if (genpd_is_cpu_domain(genpd))
2011 free_cpumask_var(genpd->cpus);
2016 static void genpd_free_data(struct generic_pm_domain *genpd)
2018 if (genpd_is_cpu_domain(genpd))
2019 free_cpumask_var(genpd->cpus);
2020 if (genpd->free_states)
2021 genpd->free_states(genpd->states, genpd->state_count);
2025 static void genpd_lock_init(struct generic_pm_domain *genpd)
2027 if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
2028 spin_lock_init(&genpd->slock);
2029 genpd->lock_ops = &genpd_spin_ops;
2031 mutex_init(&genpd->mlock);
2032 genpd->lock_ops = &genpd_mtx_ops;
2037 * pm_genpd_init - Initialize a generic I/O PM domain object.
2038 * @genpd: PM domain object to initialize.
2039 * @gov: PM domain governor to associate with the domain (may be NULL).
2040 * @is_off: Initial value of the domain's power_is_off field.
2042 * Returns 0 on successful initialization, else a negative error code.
2044 int pm_genpd_init(struct generic_pm_domain *genpd,
2045 struct dev_power_governor *gov, bool is_off)
2049 if (IS_ERR_OR_NULL(genpd))
2052 INIT_LIST_HEAD(&genpd->parent_links);
2053 INIT_LIST_HEAD(&genpd->child_links);
2054 INIT_LIST_HEAD(&genpd->dev_list);
2055 RAW_INIT_NOTIFIER_HEAD(&genpd->power_notifiers);
2056 genpd_lock_init(genpd);
2058 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
2059 atomic_set(&genpd->sd_count, 0);
2060 genpd->status = is_off ? GENPD_STATE_OFF : GENPD_STATE_ON;
2061 genpd->device_count = 0;
2062 genpd->provider = NULL;
2063 genpd->has_provider = false;
2064 genpd->accounting_time = ktime_get_mono_fast_ns();
2065 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
2066 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
2067 genpd->domain.ops.prepare = genpd_prepare;
2068 genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
2069 genpd->domain.ops.resume_noirq = genpd_resume_noirq;
2070 genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
2071 genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
2072 genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
2073 genpd->domain.ops.restore_noirq = genpd_restore_noirq;
2074 genpd->domain.ops.complete = genpd_complete;
2075 genpd->domain.start = genpd_dev_pm_start;
2077 if (genpd->flags & GENPD_FLAG_PM_CLK) {
2078 genpd->dev_ops.stop = pm_clk_suspend;
2079 genpd->dev_ops.start = pm_clk_resume;
2082 /* The always-on governor works better with the corresponding flag. */
2083 if (gov == &pm_domain_always_on_gov)
2084 genpd->flags |= GENPD_FLAG_RPM_ALWAYS_ON;
2086 /* Always-on domains must be powered on at initialization. */
2087 if ((genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd)) &&
2088 !genpd_status_on(genpd))
2091 /* Multiple states but no governor doesn't make sense. */
2092 if (!gov && genpd->state_count > 1)
2093 pr_warn("%s: no governor for states\n", genpd->name);
2095 ret = genpd_alloc_data(genpd);
2099 device_initialize(&genpd->dev);
2100 dev_set_name(&genpd->dev, "%s", genpd->name);
2102 mutex_lock(&gpd_list_lock);
2103 list_add(&genpd->gpd_list_node, &gpd_list);
2104 mutex_unlock(&gpd_list_lock);
2105 genpd_debug_add(genpd);
2109 EXPORT_SYMBOL_GPL(pm_genpd_init);
2111 static int genpd_remove(struct generic_pm_domain *genpd)
2113 struct gpd_link *l, *link;
2115 if (IS_ERR_OR_NULL(genpd))
2120 if (genpd->has_provider) {
2121 genpd_unlock(genpd);
2122 pr_err("Provider present, unable to remove %s\n", genpd->name);
2126 if (!list_empty(&genpd->parent_links) || genpd->device_count) {
2127 genpd_unlock(genpd);
2128 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
2132 list_for_each_entry_safe(link, l, &genpd->child_links, child_node) {
2133 list_del(&link->parent_node);
2134 list_del(&link->child_node);
2138 list_del(&genpd->gpd_list_node);
2139 genpd_unlock(genpd);
2140 genpd_debug_remove(genpd);
2141 cancel_work_sync(&genpd->power_off_work);
2142 genpd_free_data(genpd);
2144 pr_debug("%s: removed %s\n", __func__, genpd->name);
2150 * pm_genpd_remove - Remove a generic I/O PM domain
2151 * @genpd: Pointer to PM domain that is to be removed.
2153 * To remove the PM domain, this function:
2154 * - Removes the PM domain as a subdomain to any parent domains,
2156 * - Removes the PM domain from the list of registered PM domains.
2158 * The PM domain will only be removed, if the associated provider has
2159 * been removed, it is not a parent to any other PM domain and has no
2160 * devices associated with it.
2162 int pm_genpd_remove(struct generic_pm_domain *genpd)
2166 mutex_lock(&gpd_list_lock);
2167 ret = genpd_remove(genpd);
2168 mutex_unlock(&gpd_list_lock);
2172 EXPORT_SYMBOL_GPL(pm_genpd_remove);
2174 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
2177 * Device Tree based PM domain providers.
2179 * The code below implements generic device tree based PM domain providers that
2180 * bind device tree nodes with generic PM domains registered in the system.
2182 * Any driver that registers generic PM domains and needs to support binding of
2183 * devices to these domains is supposed to register a PM domain provider, which
2184 * maps a PM domain specifier retrieved from the device tree to a PM domain.
2186 * Two simple mapping functions have been provided for convenience:
2187 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
2188 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
2193 * struct of_genpd_provider - PM domain provider registration structure
2194 * @link: Entry in global list of PM domain providers
2195 * @node: Pointer to device tree node of PM domain provider
2196 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
2198 * @data: context pointer to be passed into @xlate callback
2200 struct of_genpd_provider {
2201 struct list_head link;
2202 struct device_node *node;
2203 genpd_xlate_t xlate;
2207 /* List of registered PM domain providers. */
2208 static LIST_HEAD(of_genpd_providers);
2209 /* Mutex to protect the list above. */
2210 static DEFINE_MUTEX(of_genpd_mutex);
2213 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
2214 * @genpdspec: OF phandle args to map into a PM domain
2215 * @data: xlate function private data - pointer to struct generic_pm_domain
2217 * This is a generic xlate function that can be used to model PM domains that
2218 * have their own device tree nodes. The private data of xlate function needs
2219 * to be a valid pointer to struct generic_pm_domain.
2221 static struct generic_pm_domain *genpd_xlate_simple(
2222 struct of_phandle_args *genpdspec,
2229 * genpd_xlate_onecell() - Xlate function using a single index.
2230 * @genpdspec: OF phandle args to map into a PM domain
2231 * @data: xlate function private data - pointer to struct genpd_onecell_data
2233 * This is a generic xlate function that can be used to model simple PM domain
2234 * controllers that have one device tree node and provide multiple PM domains.
2235 * A single cell is used as an index into an array of PM domains specified in
2236 * the genpd_onecell_data struct when registering the provider.
2238 static struct generic_pm_domain *genpd_xlate_onecell(
2239 struct of_phandle_args *genpdspec,
2242 struct genpd_onecell_data *genpd_data = data;
2243 unsigned int idx = genpdspec->args[0];
2245 if (genpdspec->args_count != 1)
2246 return ERR_PTR(-EINVAL);
2248 if (idx >= genpd_data->num_domains) {
2249 pr_err("%s: invalid domain index %u\n", __func__, idx);
2250 return ERR_PTR(-EINVAL);
2253 if (!genpd_data->domains[idx])
2254 return ERR_PTR(-ENOENT);
2256 return genpd_data->domains[idx];
2260 * genpd_add_provider() - Register a PM domain provider for a node
2261 * @np: Device node pointer associated with the PM domain provider.
2262 * @xlate: Callback for decoding PM domain from phandle arguments.
2263 * @data: Context pointer for @xlate callback.
2265 static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
2268 struct of_genpd_provider *cp;
2270 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2274 cp->node = of_node_get(np);
2277 fwnode_dev_initialized(&np->fwnode, true);
2279 mutex_lock(&of_genpd_mutex);
2280 list_add(&cp->link, &of_genpd_providers);
2281 mutex_unlock(&of_genpd_mutex);
2282 pr_debug("Added domain provider from %pOF\n", np);
2287 static bool genpd_present(const struct generic_pm_domain *genpd)
2290 const struct generic_pm_domain *gpd;
2292 mutex_lock(&gpd_list_lock);
2293 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2299 mutex_unlock(&gpd_list_lock);
2305 * of_genpd_add_provider_simple() - Register a simple PM domain provider
2306 * @np: Device node pointer associated with the PM domain provider.
2307 * @genpd: Pointer to PM domain associated with the PM domain provider.
2309 int of_genpd_add_provider_simple(struct device_node *np,
2310 struct generic_pm_domain *genpd)
2317 if (!genpd_present(genpd))
2320 genpd->dev.of_node = np;
2322 /* Parse genpd OPP table */
2323 if (genpd->set_performance_state) {
2324 ret = dev_pm_opp_of_add_table(&genpd->dev);
2326 return dev_err_probe(&genpd->dev, ret, "Failed to add OPP table\n");
2329 * Save table for faster processing while setting performance
2332 genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
2333 WARN_ON(IS_ERR(genpd->opp_table));
2336 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
2338 if (genpd->set_performance_state) {
2339 dev_pm_opp_put_opp_table(genpd->opp_table);
2340 dev_pm_opp_of_remove_table(&genpd->dev);
2346 genpd->provider = &np->fwnode;
2347 genpd->has_provider = true;
2351 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
2354 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
2355 * @np: Device node pointer associated with the PM domain provider.
2356 * @data: Pointer to the data associated with the PM domain provider.
2358 int of_genpd_add_provider_onecell(struct device_node *np,
2359 struct genpd_onecell_data *data)
2361 struct generic_pm_domain *genpd;
2369 data->xlate = genpd_xlate_onecell;
2371 for (i = 0; i < data->num_domains; i++) {
2372 genpd = data->domains[i];
2376 if (!genpd_present(genpd))
2379 genpd->dev.of_node = np;
2381 /* Parse genpd OPP table */
2382 if (genpd->set_performance_state) {
2383 ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
2385 dev_err_probe(&genpd->dev, ret,
2386 "Failed to add OPP table for index %d\n", i);
2391 * Save table for faster processing while setting
2392 * performance state.
2394 genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
2395 WARN_ON(IS_ERR(genpd->opp_table));
2398 genpd->provider = &np->fwnode;
2399 genpd->has_provider = true;
2402 ret = genpd_add_provider(np, data->xlate, data);
2410 genpd = data->domains[i];
2415 genpd->provider = NULL;
2416 genpd->has_provider = false;
2418 if (genpd->set_performance_state) {
2419 dev_pm_opp_put_opp_table(genpd->opp_table);
2420 dev_pm_opp_of_remove_table(&genpd->dev);
2426 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
2429 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2430 * @np: Device node pointer associated with the PM domain provider
2432 void of_genpd_del_provider(struct device_node *np)
2434 struct of_genpd_provider *cp, *tmp;
2435 struct generic_pm_domain *gpd;
2437 mutex_lock(&gpd_list_lock);
2438 mutex_lock(&of_genpd_mutex);
2439 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
2440 if (cp->node == np) {
2442 * For each PM domain associated with the
2443 * provider, set the 'has_provider' to false
2444 * so that the PM domain can be safely removed.
2446 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2447 if (gpd->provider == &np->fwnode) {
2448 gpd->has_provider = false;
2450 if (!gpd->set_performance_state)
2453 dev_pm_opp_put_opp_table(gpd->opp_table);
2454 dev_pm_opp_of_remove_table(&gpd->dev);
2458 fwnode_dev_initialized(&cp->node->fwnode, false);
2459 list_del(&cp->link);
2460 of_node_put(cp->node);
2465 mutex_unlock(&of_genpd_mutex);
2466 mutex_unlock(&gpd_list_lock);
2468 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2471 * genpd_get_from_provider() - Look-up PM domain
2472 * @genpdspec: OF phandle args to use for look-up
2474 * Looks for a PM domain provider under the node specified by @genpdspec and if
2475 * found, uses xlate function of the provider to map phandle args to a PM
2478 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2481 static struct generic_pm_domain *genpd_get_from_provider(
2482 struct of_phandle_args *genpdspec)
2484 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2485 struct of_genpd_provider *provider;
2488 return ERR_PTR(-EINVAL);
2490 mutex_lock(&of_genpd_mutex);
2492 /* Check if we have such a provider in our array */
2493 list_for_each_entry(provider, &of_genpd_providers, link) {
2494 if (provider->node == genpdspec->np)
2495 genpd = provider->xlate(genpdspec, provider->data);
2500 mutex_unlock(&of_genpd_mutex);
2506 * of_genpd_add_device() - Add a device to an I/O PM domain
2507 * @genpdspec: OF phandle args to use for look-up PM domain
2508 * @dev: Device to be added.
2510 * Looks-up an I/O PM domain based upon phandle args provided and adds
2511 * the device to the PM domain. Returns a negative error code on failure.
2513 int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
2515 struct generic_pm_domain *genpd;
2518 mutex_lock(&gpd_list_lock);
2520 genpd = genpd_get_from_provider(genpdspec);
2521 if (IS_ERR(genpd)) {
2522 ret = PTR_ERR(genpd);
2526 ret = genpd_add_device(genpd, dev, dev);
2529 mutex_unlock(&gpd_list_lock);
2533 EXPORT_SYMBOL_GPL(of_genpd_add_device);
2536 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2537 * @parent_spec: OF phandle args to use for parent PM domain look-up
2538 * @subdomain_spec: OF phandle args to use for subdomain look-up
2540 * Looks-up a parent PM domain and subdomain based upon phandle args
2541 * provided and adds the subdomain to the parent PM domain. Returns a
2542 * negative error code on failure.
2544 int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
2545 struct of_phandle_args *subdomain_spec)
2547 struct generic_pm_domain *parent, *subdomain;
2550 mutex_lock(&gpd_list_lock);
2552 parent = genpd_get_from_provider(parent_spec);
2553 if (IS_ERR(parent)) {
2554 ret = PTR_ERR(parent);
2558 subdomain = genpd_get_from_provider(subdomain_spec);
2559 if (IS_ERR(subdomain)) {
2560 ret = PTR_ERR(subdomain);
2564 ret = genpd_add_subdomain(parent, subdomain);
2567 mutex_unlock(&gpd_list_lock);
2569 return ret == -ENOENT ? -EPROBE_DEFER : ret;
2571 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2574 * of_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
2575 * @parent_spec: OF phandle args to use for parent PM domain look-up
2576 * @subdomain_spec: OF phandle args to use for subdomain look-up
2578 * Looks-up a parent PM domain and subdomain based upon phandle args
2579 * provided and removes the subdomain from the parent PM domain. Returns a
2580 * negative error code on failure.
2582 int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
2583 struct of_phandle_args *subdomain_spec)
2585 struct generic_pm_domain *parent, *subdomain;
2588 mutex_lock(&gpd_list_lock);
2590 parent = genpd_get_from_provider(parent_spec);
2591 if (IS_ERR(parent)) {
2592 ret = PTR_ERR(parent);
2596 subdomain = genpd_get_from_provider(subdomain_spec);
2597 if (IS_ERR(subdomain)) {
2598 ret = PTR_ERR(subdomain);
2602 ret = pm_genpd_remove_subdomain(parent, subdomain);
2605 mutex_unlock(&gpd_list_lock);
2609 EXPORT_SYMBOL_GPL(of_genpd_remove_subdomain);
2612 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2613 * @np: Pointer to device node associated with provider
2615 * Find the last PM domain that was added by a particular provider and
2616 * remove this PM domain from the list of PM domains. The provider is
2617 * identified by the 'provider' device structure that is passed. The PM
2618 * domain will only be removed, if the provider associated with domain
2621 * Returns a valid pointer to struct generic_pm_domain on success or
2622 * ERR_PTR() on failure.
2624 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2626 struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2629 if (IS_ERR_OR_NULL(np))
2630 return ERR_PTR(-EINVAL);
2632 mutex_lock(&gpd_list_lock);
2633 list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2634 if (gpd->provider == &np->fwnode) {
2635 ret = genpd_remove(gpd);
2636 genpd = ret ? ERR_PTR(ret) : gpd;
2640 mutex_unlock(&gpd_list_lock);
2644 EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2646 static void genpd_release_dev(struct device *dev)
2648 of_node_put(dev->of_node);
2652 static struct bus_type genpd_bus_type = {
2657 * genpd_dev_pm_detach - Detach a device from its PM domain.
2658 * @dev: Device to detach.
2659 * @power_off: Currently not used
2661 * Try to locate a corresponding generic PM domain, which the device was
2662 * attached to previously. If such is found, the device is detached from it.
2664 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2666 struct generic_pm_domain *pd;
2670 pd = dev_to_genpd(dev);
2674 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2676 /* Drop the default performance state */
2677 if (dev_gpd_data(dev)->default_pstate) {
2678 dev_pm_genpd_set_performance_state(dev, 0);
2679 dev_gpd_data(dev)->default_pstate = 0;
2682 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2683 ret = genpd_remove_device(pd, dev);
2692 dev_err(dev, "failed to remove from PM domain %s: %d",
2697 /* Check if PM domain can be powered off after removing this device. */
2698 genpd_queue_power_off_work(pd);
2700 /* Unregister the device if it was created by genpd. */
2701 if (dev->bus == &genpd_bus_type)
2702 device_unregister(dev);
2705 static void genpd_dev_pm_sync(struct device *dev)
2707 struct generic_pm_domain *pd;
2709 pd = dev_to_genpd(dev);
2713 genpd_queue_power_off_work(pd);
2716 static int __genpd_dev_pm_attach(struct device *dev, struct device *base_dev,
2717 unsigned int index, bool power_on)
2719 struct of_phandle_args pd_args;
2720 struct generic_pm_domain *pd;
2724 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2725 "#power-domain-cells", index, &pd_args);
2729 mutex_lock(&gpd_list_lock);
2730 pd = genpd_get_from_provider(&pd_args);
2731 of_node_put(pd_args.np);
2733 mutex_unlock(&gpd_list_lock);
2734 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2735 __func__, PTR_ERR(pd));
2739 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2741 ret = genpd_add_device(pd, dev, base_dev);
2742 mutex_unlock(&gpd_list_lock);
2745 return dev_err_probe(dev, ret, "failed to add to PM domain %s\n", pd->name);
2747 dev->pm_domain->detach = genpd_dev_pm_detach;
2748 dev->pm_domain->sync = genpd_dev_pm_sync;
2752 ret = genpd_power_on(pd, 0);
2757 genpd_remove_device(pd, dev);
2758 return -EPROBE_DEFER;
2761 /* Set the default performance state */
2762 pstate = of_get_required_opp_performance_state(dev->of_node, index);
2763 if (pstate < 0 && pstate != -ENODEV && pstate != -EOPNOTSUPP) {
2766 } else if (pstate > 0) {
2767 ret = dev_pm_genpd_set_performance_state(dev, pstate);
2770 dev_gpd_data(dev)->default_pstate = pstate;
2775 dev_err(dev, "failed to set required performance state for power-domain %s: %d\n",
2777 genpd_remove_device(pd, dev);
2782 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2783 * @dev: Device to attach.
2785 * Parse device's OF node to find a PM domain specifier. If such is found,
2786 * attaches the device to retrieved pm_domain ops.
2788 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2789 * PM domain or when multiple power-domains exists for it, else a negative error
2790 * code. Note that if a power-domain exists for the device, but it cannot be
2791 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2792 * not probed and to re-try again later.
2794 int genpd_dev_pm_attach(struct device *dev)
2800 * Devices with multiple PM domains must be attached separately, as we
2801 * can only attach one PM domain per device.
2803 if (of_count_phandle_with_args(dev->of_node, "power-domains",
2804 "#power-domain-cells") != 1)
2807 return __genpd_dev_pm_attach(dev, dev, 0, true);
2809 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2812 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2813 * @dev: The device used to lookup the PM domain.
2814 * @index: The index of the PM domain.
2816 * Parse device's OF node to find a PM domain specifier at the provided @index.
2817 * If such is found, creates a virtual device and attaches it to the retrieved
2818 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2819 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2821 * Returns the created virtual device if successfully attached PM domain, NULL
2822 * when the device don't need a PM domain, else an ERR_PTR() in case of
2823 * failures. If a power-domain exists for the device, but cannot be found or
2824 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2825 * is not probed and to re-try again later.
2827 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
2830 struct device *virt_dev;
2837 /* Verify that the index is within a valid range. */
2838 num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
2839 "#power-domain-cells");
2840 if (index >= num_domains)
2843 /* Allocate and register device on the genpd bus. */
2844 virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL);
2846 return ERR_PTR(-ENOMEM);
2848 dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev));
2849 virt_dev->bus = &genpd_bus_type;
2850 virt_dev->release = genpd_release_dev;
2851 virt_dev->of_node = of_node_get(dev->of_node);
2853 ret = device_register(virt_dev);
2855 put_device(virt_dev);
2856 return ERR_PTR(ret);
2859 /* Try to attach the device to the PM domain at the specified index. */
2860 ret = __genpd_dev_pm_attach(virt_dev, dev, index, false);
2862 device_unregister(virt_dev);
2863 return ret ? ERR_PTR(ret) : NULL;
2866 pm_runtime_enable(virt_dev);
2867 genpd_queue_power_off_work(dev_to_genpd(virt_dev));
2871 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
2874 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
2875 * @dev: The device used to lookup the PM domain.
2876 * @name: The name of the PM domain.
2878 * Parse device's OF node to find a PM domain specifier using the
2879 * power-domain-names DT property. For further description see
2880 * genpd_dev_pm_attach_by_id().
2882 struct device *genpd_dev_pm_attach_by_name(struct device *dev, const char *name)
2889 index = of_property_match_string(dev->of_node, "power-domain-names",
2894 return genpd_dev_pm_attach_by_id(dev, index);
2897 static const struct of_device_id idle_state_match[] = {
2898 { .compatible = "domain-idle-state", },
2902 static int genpd_parse_state(struct genpd_power_state *genpd_state,
2903 struct device_node *state_node)
2907 u32 entry_latency, exit_latency;
2909 err = of_property_read_u32(state_node, "entry-latency-us",
2912 pr_debug(" * %pOF missing entry-latency-us property\n",
2917 err = of_property_read_u32(state_node, "exit-latency-us",
2920 pr_debug(" * %pOF missing exit-latency-us property\n",
2925 err = of_property_read_u32(state_node, "min-residency-us", &residency);
2927 genpd_state->residency_ns = 1000 * residency;
2929 genpd_state->power_on_latency_ns = 1000 * exit_latency;
2930 genpd_state->power_off_latency_ns = 1000 * entry_latency;
2931 genpd_state->fwnode = &state_node->fwnode;
2936 static int genpd_iterate_idle_states(struct device_node *dn,
2937 struct genpd_power_state *states)
2940 struct of_phandle_iterator it;
2941 struct device_node *np;
2944 ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2946 return ret == -ENOENT ? 0 : ret;
2948 /* Loop over the phandles until all the requested entry is found */
2949 of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
2951 if (!of_match_node(idle_state_match, np))
2954 ret = genpd_parse_state(&states[i], np);
2956 pr_err("Parsing idle state node %pOF failed with err %d\n",
2969 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2971 * @dn: The genpd device node
2972 * @states: The pointer to which the state array will be saved.
2973 * @n: The count of elements in the array returned from this function.
2975 * Returns the device states parsed from the OF node. The memory for the states
2976 * is allocated by this function and is the responsibility of the caller to
2977 * free the memory after use. If any or zero compatible domain idle states is
2978 * found it returns 0 and in case of errors, a negative error code is returned.
2980 int of_genpd_parse_idle_states(struct device_node *dn,
2981 struct genpd_power_state **states, int *n)
2983 struct genpd_power_state *st;
2986 ret = genpd_iterate_idle_states(dn, NULL);
2996 st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
3000 ret = genpd_iterate_idle_states(dn, st);
3003 return ret < 0 ? ret : -EINVAL;
3011 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
3014 * pm_genpd_opp_to_performance_state - Gets performance state of the genpd from its OPP node.
3016 * @genpd_dev: Genpd's device for which the performance-state needs to be found.
3017 * @opp: struct dev_pm_opp of the OPP for which we need to find performance
3020 * Returns performance state encoded in the OPP of the genpd. This calls
3021 * platform specific genpd->opp_to_performance_state() callback to translate
3022 * power domain OPP to performance state.
3024 * Returns performance state on success and 0 on failure.
3026 unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
3027 struct dev_pm_opp *opp)
3029 struct generic_pm_domain *genpd = NULL;
3032 genpd = container_of(genpd_dev, struct generic_pm_domain, dev);
3034 if (unlikely(!genpd->opp_to_performance_state))
3038 state = genpd->opp_to_performance_state(genpd, opp);
3039 genpd_unlock(genpd);
3043 EXPORT_SYMBOL_GPL(pm_genpd_opp_to_performance_state);
3045 static int __init genpd_bus_init(void)
3047 return bus_register(&genpd_bus_type);
3049 core_initcall(genpd_bus_init);
3051 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
3054 /*** debugfs support ***/
3056 #ifdef CONFIG_DEBUG_FS
3058 * TODO: This function is a slightly modified version of rtpm_status_show
3059 * from sysfs.c, so generalize it.
3061 static void rtpm_status_str(struct seq_file *s, struct device *dev)
3063 static const char * const status_lookup[] = {
3064 [RPM_ACTIVE] = "active",
3065 [RPM_RESUMING] = "resuming",
3066 [RPM_SUSPENDED] = "suspended",
3067 [RPM_SUSPENDING] = "suspending"
3071 if (dev->power.runtime_error)
3073 else if (dev->power.disable_depth)
3075 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
3076 p = status_lookup[dev->power.runtime_status];
3080 seq_printf(s, "%-25s ", p);
3083 static void perf_status_str(struct seq_file *s, struct device *dev)
3085 struct generic_pm_domain_data *gpd_data;
3087 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
3088 seq_put_decimal_ull(s, "", gpd_data->performance_state);
3091 static int genpd_summary_one(struct seq_file *s,
3092 struct generic_pm_domain *genpd)
3094 static const char * const status_lookup[] = {
3095 [GENPD_STATE_ON] = "on",
3096 [GENPD_STATE_OFF] = "off"
3098 struct pm_domain_data *pm_data;
3099 const char *kobj_path;
3100 struct gpd_link *link;
3104 ret = genpd_lock_interruptible(genpd);
3106 return -ERESTARTSYS;
3108 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
3110 if (!genpd_status_on(genpd))
3111 snprintf(state, sizeof(state), "%s-%u",
3112 status_lookup[genpd->status], genpd->state_idx);
3114 snprintf(state, sizeof(state), "%s",
3115 status_lookup[genpd->status]);
3116 seq_printf(s, "%-30s %-50s %u", genpd->name, state, genpd->performance_state);
3119 * Modifications on the list require holding locks on both
3120 * parent and child, so we are safe.
3121 * Also genpd->name is immutable.
3123 list_for_each_entry(link, &genpd->parent_links, parent_node) {
3124 if (list_is_first(&link->parent_node, &genpd->parent_links))
3125 seq_printf(s, "\n%48s", " ");
3126 seq_printf(s, "%s", link->child->name);
3127 if (!list_is_last(&link->parent_node, &genpd->parent_links))
3131 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
3132 kobj_path = kobject_get_path(&pm_data->dev->kobj,
3133 genpd_is_irq_safe(genpd) ?
3134 GFP_ATOMIC : GFP_KERNEL);
3135 if (kobj_path == NULL)
3138 seq_printf(s, "\n %-50s ", kobj_path);
3139 rtpm_status_str(s, pm_data->dev);
3140 perf_status_str(s, pm_data->dev);
3146 genpd_unlock(genpd);
3151 static int summary_show(struct seq_file *s, void *data)
3153 struct generic_pm_domain *genpd;
3156 seq_puts(s, "domain status children performance\n");
3157 seq_puts(s, " /device runtime status\n");
3158 seq_puts(s, "----------------------------------------------------------------------------------------------\n");
3160 ret = mutex_lock_interruptible(&gpd_list_lock);
3162 return -ERESTARTSYS;
3164 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
3165 ret = genpd_summary_one(s, genpd);
3169 mutex_unlock(&gpd_list_lock);
3174 static int status_show(struct seq_file *s, void *data)
3176 static const char * const status_lookup[] = {
3177 [GENPD_STATE_ON] = "on",
3178 [GENPD_STATE_OFF] = "off"
3181 struct generic_pm_domain *genpd = s->private;
3184 ret = genpd_lock_interruptible(genpd);
3186 return -ERESTARTSYS;
3188 if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
3191 if (genpd->status == GENPD_STATE_OFF)
3192 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
3195 seq_printf(s, "%s\n", status_lookup[genpd->status]);
3197 genpd_unlock(genpd);
3201 static int sub_domains_show(struct seq_file *s, void *data)
3203 struct generic_pm_domain *genpd = s->private;
3204 struct gpd_link *link;
3207 ret = genpd_lock_interruptible(genpd);
3209 return -ERESTARTSYS;
3211 list_for_each_entry(link, &genpd->parent_links, parent_node)
3212 seq_printf(s, "%s\n", link->child->name);
3214 genpd_unlock(genpd);
3218 static int idle_states_show(struct seq_file *s, void *data)
3220 struct generic_pm_domain *genpd = s->private;
3221 u64 now, delta, idle_time = 0;
3225 ret = genpd_lock_interruptible(genpd);
3227 return -ERESTARTSYS;
3229 seq_puts(s, "State Time Spent(ms) Usage Rejected\n");
3231 for (i = 0; i < genpd->state_count; i++) {
3232 idle_time += genpd->states[i].idle_time;
3234 if (genpd->status == GENPD_STATE_OFF && genpd->state_idx == i) {
3235 now = ktime_get_mono_fast_ns();
3236 if (now > genpd->accounting_time) {
3237 delta = now - genpd->accounting_time;
3242 do_div(idle_time, NSEC_PER_MSEC);
3243 seq_printf(s, "S%-13i %-14llu %-14llu %llu\n", i, idle_time,
3244 genpd->states[i].usage, genpd->states[i].rejected);
3247 genpd_unlock(genpd);
3251 static int active_time_show(struct seq_file *s, void *data)
3253 struct generic_pm_domain *genpd = s->private;
3254 u64 now, on_time, delta = 0;
3257 ret = genpd_lock_interruptible(genpd);
3259 return -ERESTARTSYS;
3261 if (genpd->status == GENPD_STATE_ON) {
3262 now = ktime_get_mono_fast_ns();
3263 if (now > genpd->accounting_time)
3264 delta = now - genpd->accounting_time;
3267 on_time = genpd->on_time + delta;
3268 do_div(on_time, NSEC_PER_MSEC);
3269 seq_printf(s, "%llu ms\n", on_time);
3271 genpd_unlock(genpd);
3275 static int total_idle_time_show(struct seq_file *s, void *data)
3277 struct generic_pm_domain *genpd = s->private;
3278 u64 now, delta, total = 0;
3282 ret = genpd_lock_interruptible(genpd);
3284 return -ERESTARTSYS;
3286 for (i = 0; i < genpd->state_count; i++) {
3287 total += genpd->states[i].idle_time;
3289 if (genpd->status == GENPD_STATE_OFF && genpd->state_idx == i) {
3290 now = ktime_get_mono_fast_ns();
3291 if (now > genpd->accounting_time) {
3292 delta = now - genpd->accounting_time;
3298 do_div(total, NSEC_PER_MSEC);
3299 seq_printf(s, "%llu ms\n", total);
3301 genpd_unlock(genpd);
3306 static int devices_show(struct seq_file *s, void *data)
3308 struct generic_pm_domain *genpd = s->private;
3309 struct pm_domain_data *pm_data;
3310 const char *kobj_path;
3313 ret = genpd_lock_interruptible(genpd);
3315 return -ERESTARTSYS;
3317 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
3318 kobj_path = kobject_get_path(&pm_data->dev->kobj,
3319 genpd_is_irq_safe(genpd) ?
3320 GFP_ATOMIC : GFP_KERNEL);
3321 if (kobj_path == NULL)
3324 seq_printf(s, "%s\n", kobj_path);
3328 genpd_unlock(genpd);
3332 static int perf_state_show(struct seq_file *s, void *data)
3334 struct generic_pm_domain *genpd = s->private;
3336 if (genpd_lock_interruptible(genpd))
3337 return -ERESTARTSYS;
3339 seq_printf(s, "%u\n", genpd->performance_state);
3341 genpd_unlock(genpd);
3345 DEFINE_SHOW_ATTRIBUTE(summary);
3346 DEFINE_SHOW_ATTRIBUTE(status);
3347 DEFINE_SHOW_ATTRIBUTE(sub_domains);
3348 DEFINE_SHOW_ATTRIBUTE(idle_states);
3349 DEFINE_SHOW_ATTRIBUTE(active_time);
3350 DEFINE_SHOW_ATTRIBUTE(total_idle_time);
3351 DEFINE_SHOW_ATTRIBUTE(devices);
3352 DEFINE_SHOW_ATTRIBUTE(perf_state);
3354 static void genpd_debug_add(struct generic_pm_domain *genpd)
3358 if (!genpd_debugfs_dir)
3361 d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
3363 debugfs_create_file("current_state", 0444,
3364 d, genpd, &status_fops);
3365 debugfs_create_file("sub_domains", 0444,
3366 d, genpd, &sub_domains_fops);
3367 debugfs_create_file("idle_states", 0444,
3368 d, genpd, &idle_states_fops);
3369 debugfs_create_file("active_time", 0444,
3370 d, genpd, &active_time_fops);
3371 debugfs_create_file("total_idle_time", 0444,
3372 d, genpd, &total_idle_time_fops);
3373 debugfs_create_file("devices", 0444,
3374 d, genpd, &devices_fops);
3375 if (genpd->set_performance_state)
3376 debugfs_create_file("perf_state", 0444,
3377 d, genpd, &perf_state_fops);
3380 static int __init genpd_debug_init(void)
3382 struct generic_pm_domain *genpd;
3384 genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
3386 debugfs_create_file("pm_genpd_summary", S_IRUGO, genpd_debugfs_dir,
3387 NULL, &summary_fops);
3389 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
3390 genpd_debug_add(genpd);
3394 late_initcall(genpd_debug_init);
3396 static void __exit genpd_debug_exit(void)
3398 debugfs_remove_recursive(genpd_debugfs_dir);
3400 __exitcall(genpd_debug_exit);
3401 #endif /* CONFIG_DEBUG_FS */