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>
26 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
28 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
30 type (*__routine)(struct device *__d); \
31 type __ret = (type)0; \
33 __routine = genpd->dev_ops.callback; \
35 __ret = __routine(dev); \
40 static LIST_HEAD(gpd_list);
41 static DEFINE_MUTEX(gpd_list_lock);
43 struct genpd_lock_ops {
44 void (*lock)(struct generic_pm_domain *genpd);
45 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
46 int (*lock_interruptible)(struct generic_pm_domain *genpd);
47 void (*unlock)(struct generic_pm_domain *genpd);
50 static void genpd_lock_mtx(struct generic_pm_domain *genpd)
52 mutex_lock(&genpd->mlock);
55 static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
58 mutex_lock_nested(&genpd->mlock, depth);
61 static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
63 return mutex_lock_interruptible(&genpd->mlock);
66 static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
68 return mutex_unlock(&genpd->mlock);
71 static const struct genpd_lock_ops genpd_mtx_ops = {
72 .lock = genpd_lock_mtx,
73 .lock_nested = genpd_lock_nested_mtx,
74 .lock_interruptible = genpd_lock_interruptible_mtx,
75 .unlock = genpd_unlock_mtx,
78 static void genpd_lock_spin(struct generic_pm_domain *genpd)
79 __acquires(&genpd->slock)
83 spin_lock_irqsave(&genpd->slock, flags);
84 genpd->lock_flags = flags;
87 static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
89 __acquires(&genpd->slock)
93 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
94 genpd->lock_flags = flags;
97 static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
98 __acquires(&genpd->slock)
102 spin_lock_irqsave(&genpd->slock, flags);
103 genpd->lock_flags = flags;
107 static void genpd_unlock_spin(struct generic_pm_domain *genpd)
108 __releases(&genpd->slock)
110 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
113 static const struct genpd_lock_ops genpd_spin_ops = {
114 .lock = genpd_lock_spin,
115 .lock_nested = genpd_lock_nested_spin,
116 .lock_interruptible = genpd_lock_interruptible_spin,
117 .unlock = genpd_unlock_spin,
120 #define genpd_lock(p) p->lock_ops->lock(p)
121 #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
122 #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
123 #define genpd_unlock(p) p->lock_ops->unlock(p)
125 #define genpd_status_on(genpd) (genpd->status == GENPD_STATE_ON)
126 #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
127 #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
128 #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
129 #define genpd_is_cpu_domain(genpd) (genpd->flags & GENPD_FLAG_CPU_DOMAIN)
130 #define genpd_is_rpm_always_on(genpd) (genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
131 #define genpd_is_opp_table_fw(genpd) (genpd->flags & GENPD_FLAG_OPP_TABLE_FW)
133 static inline bool irq_safe_dev_in_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 domain, which
142 * callbacks are allowed to sleep. This indicates a suboptimal
143 * configuration for PM, but it doesn't matter for an always on domain.
145 if (genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd))
149 dev_warn_once(dev, "PM domain %s will not be powered off\n",
155 static int genpd_runtime_suspend(struct device *dev);
158 * Get the generic PM domain for a particular struct device.
159 * This validates the struct device pointer, the PM domain pointer,
160 * and checks that the PM domain pointer is a real generic PM domain.
161 * Any failure results in NULL being returned.
163 static struct generic_pm_domain *dev_to_genpd_safe(struct device *dev)
165 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
168 /* A genpd's always have its ->runtime_suspend() callback assigned. */
169 if (dev->pm_domain->ops.runtime_suspend == genpd_runtime_suspend)
170 return pd_to_genpd(dev->pm_domain);
176 * This should only be used where we are certain that the pm_domain
177 * attached to the device is a genpd domain.
179 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
181 if (IS_ERR_OR_NULL(dev->pm_domain))
182 return ERR_PTR(-EINVAL);
184 return pd_to_genpd(dev->pm_domain);
187 struct device *dev_to_genpd_dev(struct device *dev)
189 struct generic_pm_domain *genpd = dev_to_genpd(dev);
192 return ERR_CAST(genpd);
197 static int genpd_stop_dev(const struct generic_pm_domain *genpd,
200 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
203 static int genpd_start_dev(const struct generic_pm_domain *genpd,
206 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
209 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
213 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
214 ret = !!atomic_dec_and_test(&genpd->sd_count);
219 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
221 atomic_inc(&genpd->sd_count);
222 smp_mb__after_atomic();
225 #ifdef CONFIG_DEBUG_FS
226 static struct dentry *genpd_debugfs_dir;
228 static void genpd_debug_add(struct generic_pm_domain *genpd);
230 static void genpd_debug_remove(struct generic_pm_domain *genpd)
232 if (!genpd_debugfs_dir)
235 debugfs_lookup_and_remove(genpd->name, genpd_debugfs_dir);
238 static void genpd_update_accounting(struct generic_pm_domain *genpd)
242 now = ktime_get_mono_fast_ns();
243 if (now <= genpd->accounting_time)
246 delta = now - genpd->accounting_time;
249 * If genpd->status is active, it means we are just
250 * out of off and so update the idle time and vice
253 if (genpd->status == GENPD_STATE_ON)
254 genpd->states[genpd->state_idx].idle_time += delta;
256 genpd->on_time += delta;
258 genpd->accounting_time = now;
261 static inline void genpd_debug_add(struct generic_pm_domain *genpd) {}
262 static inline void genpd_debug_remove(struct generic_pm_domain *genpd) {}
263 static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
266 static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd,
269 struct generic_pm_domain_data *pd_data;
270 struct pm_domain_data *pdd;
271 struct gpd_link *link;
273 /* New requested state is same as Max requested state */
274 if (state == genpd->performance_state)
277 /* New requested state is higher than Max requested state */
278 if (state > genpd->performance_state)
281 /* Traverse all devices within the domain */
282 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
283 pd_data = to_gpd_data(pdd);
285 if (pd_data->performance_state > state)
286 state = pd_data->performance_state;
290 * Traverse all sub-domains within the domain. This can be
291 * done without any additional locking as the link->performance_state
292 * field is protected by the parent genpd->lock, which is already taken.
294 * Also note that link->performance_state (subdomain's performance state
295 * requirement to parent domain) is different from
296 * link->child->performance_state (current performance state requirement
297 * of the devices/sub-domains of the subdomain) and so can have a
300 * Note that we also take vote from powered-off sub-domains into account
301 * as the same is done for devices right now.
303 list_for_each_entry(link, &genpd->parent_links, parent_node) {
304 if (link->performance_state > state)
305 state = link->performance_state;
311 static int genpd_xlate_performance_state(struct generic_pm_domain *genpd,
312 struct generic_pm_domain *parent,
315 if (!parent->set_performance_state)
318 return dev_pm_opp_xlate_performance_state(genpd->opp_table,
323 static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
324 unsigned int state, int depth);
326 static void _genpd_rollback_parent_state(struct gpd_link *link, int depth)
328 struct generic_pm_domain *parent = link->parent;
331 genpd_lock_nested(parent, depth + 1);
333 parent_state = link->prev_performance_state;
334 link->performance_state = parent_state;
336 parent_state = _genpd_reeval_performance_state(parent, parent_state);
337 if (_genpd_set_performance_state(parent, parent_state, depth + 1)) {
338 pr_err("%s: Failed to roll back to %d performance state\n",
339 parent->name, parent_state);
342 genpd_unlock(parent);
345 static int _genpd_set_parent_state(struct generic_pm_domain *genpd,
346 struct gpd_link *link,
347 unsigned int state, int depth)
349 struct generic_pm_domain *parent = link->parent;
350 int parent_state, ret;
352 /* Find parent's performance state */
353 ret = genpd_xlate_performance_state(genpd, parent, state);
354 if (unlikely(ret < 0))
359 genpd_lock_nested(parent, depth + 1);
361 link->prev_performance_state = link->performance_state;
362 link->performance_state = parent_state;
364 parent_state = _genpd_reeval_performance_state(parent, parent_state);
365 ret = _genpd_set_performance_state(parent, parent_state, depth + 1);
367 link->performance_state = link->prev_performance_state;
369 genpd_unlock(parent);
374 static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
375 unsigned int state, int depth)
377 struct gpd_link *link = NULL;
380 if (state == genpd->performance_state)
383 /* When scaling up, propagate to parents first in normal order */
384 if (state > genpd->performance_state) {
385 list_for_each_entry(link, &genpd->child_links, child_node) {
386 ret = _genpd_set_parent_state(genpd, link, state, depth);
388 goto rollback_parents_up;
392 if (genpd->set_performance_state) {
393 ret = genpd->set_performance_state(genpd, state);
396 goto rollback_parents_up;
401 /* When scaling down, propagate to parents last in reverse order */
402 if (state < genpd->performance_state) {
403 list_for_each_entry_reverse(link, &genpd->child_links, child_node) {
404 ret = _genpd_set_parent_state(genpd, link, state, depth);
406 goto rollback_parents_down;
410 genpd->performance_state = state;
414 list_for_each_entry_continue_reverse(link, &genpd->child_links, child_node)
415 _genpd_rollback_parent_state(link, depth);
417 rollback_parents_down:
418 list_for_each_entry_continue(link, &genpd->child_links, child_node)
419 _genpd_rollback_parent_state(link, depth);
423 static int genpd_set_performance_state(struct device *dev, unsigned int state)
425 struct generic_pm_domain *genpd = dev_to_genpd(dev);
426 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
427 unsigned int prev_state;
430 prev_state = gpd_data->performance_state;
431 if (prev_state == state)
434 gpd_data->performance_state = state;
435 state = _genpd_reeval_performance_state(genpd, state);
437 ret = _genpd_set_performance_state(genpd, state, 0);
439 gpd_data->performance_state = prev_state;
444 static int genpd_drop_performance_state(struct device *dev)
446 unsigned int prev_state = dev_gpd_data(dev)->performance_state;
448 if (!genpd_set_performance_state(dev, 0))
454 static void genpd_restore_performance_state(struct device *dev,
458 genpd_set_performance_state(dev, state);
461 static int genpd_dev_pm_set_performance_state(struct device *dev,
464 struct generic_pm_domain *genpd = dev_to_genpd(dev);
468 if (pm_runtime_suspended(dev)) {
469 dev_gpd_data(dev)->rpm_pstate = state;
471 ret = genpd_set_performance_state(dev, state);
473 dev_gpd_data(dev)->rpm_pstate = 0;
481 * dev_pm_genpd_set_performance_state- Set performance state of device's power
484 * @dev: Device for which the performance-state needs to be set.
485 * @state: Target performance state of the device. This can be set as 0 when the
486 * device doesn't have any performance state constraints left (And so
487 * the device wouldn't participate anymore to find the target
488 * performance state of the genpd).
490 * It is assumed that the users guarantee that the genpd wouldn't be detached
491 * while this routine is getting called.
493 * Returns 0 on success and negative error values on failures.
495 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
497 struct generic_pm_domain *genpd;
499 genpd = dev_to_genpd_safe(dev);
503 if (WARN_ON(!dev->power.subsys_data ||
504 !dev->power.subsys_data->domain_data))
507 return genpd_dev_pm_set_performance_state(dev, state);
509 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
512 * dev_pm_genpd_set_next_wakeup - Notify PM framework of an impending wakeup.
514 * @dev: Device to handle
515 * @next: impending interrupt/wakeup for the device
518 * Allow devices to inform of the next wakeup. It's assumed that the users
519 * guarantee that the genpd wouldn't be detached while this routine is getting
520 * called. Additionally, it's also assumed that @dev isn't runtime suspended
522 * Although devices are expected to update the next_wakeup after the end of
523 * their usecase as well, it is possible the devices themselves may not know
524 * about that, so stale @next will be ignored when powering off the domain.
526 void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
528 struct generic_pm_domain *genpd;
529 struct gpd_timing_data *td;
531 genpd = dev_to_genpd_safe(dev);
535 td = to_gpd_data(dev->power.subsys_data->domain_data)->td;
537 td->next_wakeup = next;
539 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_next_wakeup);
542 * dev_pm_genpd_get_next_hrtimer - Return the next_hrtimer for the genpd
543 * @dev: A device that is attached to the genpd.
545 * This routine should typically be called for a device, at the point of when a
546 * GENPD_NOTIFY_PRE_OFF notification has been sent for it.
548 * Returns the aggregated value of the genpd's next hrtimer or KTIME_MAX if no
549 * valid value have been set.
551 ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev)
553 struct generic_pm_domain *genpd;
555 genpd = dev_to_genpd_safe(dev);
560 return genpd->gd->next_hrtimer;
564 EXPORT_SYMBOL_GPL(dev_pm_genpd_get_next_hrtimer);
567 * dev_pm_genpd_synced_poweroff - Next power off should be synchronous
569 * @dev: A device that is attached to the genpd.
571 * Allows a consumer of the genpd to notify the provider that the next power off
572 * should be synchronous.
574 * It is assumed that the users guarantee that the genpd wouldn't be detached
575 * while this routine is getting called.
577 void dev_pm_genpd_synced_poweroff(struct device *dev)
579 struct generic_pm_domain *genpd;
581 genpd = dev_to_genpd_safe(dev);
586 genpd->synced_poweroff = true;
589 EXPORT_SYMBOL_GPL(dev_pm_genpd_synced_poweroff);
592 * dev_pm_genpd_set_hwmode() - Set the HW mode for the device and its PM domain.
594 * @dev: Device for which the HW-mode should be changed.
595 * @enable: Value to set or unset the HW-mode.
597 * Some PM domains can rely on HW signals to control the power for a device. To
598 * allow a consumer driver to switch the behaviour for its device in runtime,
599 * which may be beneficial from a latency or energy point of view, this function
602 * It is assumed that the users guarantee that the genpd wouldn't be detached
603 * while this routine is getting called.
605 * Return: Returns 0 on success and negative error values on failures.
607 int dev_pm_genpd_set_hwmode(struct device *dev, bool enable)
609 struct generic_pm_domain *genpd;
612 genpd = dev_to_genpd_safe(dev);
616 if (!genpd->set_hwmode_dev)
621 if (dev_gpd_data(dev)->hw_mode == enable)
624 ret = genpd->set_hwmode_dev(genpd, dev, enable);
626 dev_gpd_data(dev)->hw_mode = enable;
632 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_hwmode);
635 * dev_pm_genpd_get_hwmode() - Get the HW mode setting for the device.
637 * @dev: Device for which the current HW-mode setting should be fetched.
639 * This helper function allows consumer drivers to fetch the current HW mode
640 * setting of its the device.
642 * It is assumed that the users guarantee that the genpd wouldn't be detached
643 * while this routine is getting called.
645 * Return: Returns the HW mode setting of device from SW cached hw_mode.
647 bool dev_pm_genpd_get_hwmode(struct device *dev)
649 return dev_gpd_data(dev)->hw_mode;
651 EXPORT_SYMBOL_GPL(dev_pm_genpd_get_hwmode);
653 static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
655 unsigned int state_idx = genpd->state_idx;
660 /* Notify consumers that we are about to power on. */
661 ret = raw_notifier_call_chain_robust(&genpd->power_notifiers,
663 GENPD_NOTIFY_OFF, NULL);
664 ret = notifier_to_errno(ret);
668 if (!genpd->power_on)
671 timed = timed && genpd->gd && !genpd->states[state_idx].fwnode;
673 ret = genpd->power_on(genpd);
680 time_start = ktime_get();
681 ret = genpd->power_on(genpd);
685 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
686 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
689 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
690 genpd->gd->max_off_time_changed = true;
691 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
692 genpd->name, "on", elapsed_ns);
695 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_ON, NULL);
696 genpd->synced_poweroff = false;
699 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_OFF,
704 static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
706 unsigned int state_idx = genpd->state_idx;
711 /* Notify consumers that we are about to power off. */
712 ret = raw_notifier_call_chain_robust(&genpd->power_notifiers,
713 GENPD_NOTIFY_PRE_OFF,
714 GENPD_NOTIFY_ON, NULL);
715 ret = notifier_to_errno(ret);
719 if (!genpd->power_off)
722 timed = timed && genpd->gd && !genpd->states[state_idx].fwnode;
724 ret = genpd->power_off(genpd);
731 time_start = ktime_get();
732 ret = genpd->power_off(genpd);
736 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
737 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
740 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
741 genpd->gd->max_off_time_changed = true;
742 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
743 genpd->name, "off", elapsed_ns);
746 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_OFF,
750 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_ON, NULL);
755 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
756 * @genpd: PM domain to power off.
758 * Queue up the execution of genpd_power_off() unless it's already been done
761 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
763 queue_work(pm_wq, &genpd->power_off_work);
767 * genpd_power_off - Remove power from a given PM domain.
768 * @genpd: PM domain to power down.
769 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
770 * RPM status of the releated device is in an intermediate state, not yet turned
771 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
772 * be RPM_SUSPENDED, while it tries to power off the PM domain.
773 * @depth: nesting count for lockdep.
775 * If all of the @genpd's devices have been suspended and all of its subdomains
776 * have been powered down, remove power from @genpd.
778 static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
781 struct pm_domain_data *pdd;
782 struct gpd_link *link;
783 unsigned int not_suspended = 0;
787 * Do not try to power off the domain in the following situations:
788 * (1) The domain is already in the "power off" state.
789 * (2) System suspend is in progress.
791 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
795 * Abort power off for the PM domain in the following situations:
796 * (1) The domain is configured as always on.
797 * (2) When the domain has a subdomain being powered on.
799 if (genpd_is_always_on(genpd) ||
800 genpd_is_rpm_always_on(genpd) ||
801 atomic_read(&genpd->sd_count) > 0)
805 * The children must be in their deepest (powered-off) states to allow
806 * the parent to be powered off. Note that, there's no need for
807 * additional locking, as powering on a child, requires the parent's
808 * lock to be acquired first.
810 list_for_each_entry(link, &genpd->parent_links, parent_node) {
811 struct generic_pm_domain *child = link->child;
812 if (child->state_idx < child->state_count - 1)
816 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
818 * Do not allow PM domain to be powered off, when an IRQ safe
819 * device is part of a non-IRQ safe domain.
821 if (!pm_runtime_suspended(pdd->dev) ||
822 irq_safe_dev_in_sleep_domain(pdd->dev, genpd))
826 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
829 if (genpd->gov && genpd->gov->power_down_ok) {
830 if (!genpd->gov->power_down_ok(&genpd->domain))
834 /* Default to shallowest state. */
836 genpd->state_idx = 0;
838 /* Don't power off, if a child domain is waiting to power on. */
839 if (atomic_read(&genpd->sd_count) > 0)
842 ret = _genpd_power_off(genpd, true);
844 genpd->states[genpd->state_idx].rejected++;
848 genpd->status = GENPD_STATE_OFF;
849 genpd_update_accounting(genpd);
850 genpd->states[genpd->state_idx].usage++;
852 list_for_each_entry(link, &genpd->child_links, child_node) {
853 genpd_sd_counter_dec(link->parent);
854 genpd_lock_nested(link->parent, depth + 1);
855 genpd_power_off(link->parent, false, depth + 1);
856 genpd_unlock(link->parent);
863 * genpd_power_on - Restore power to a given PM domain and its parents.
864 * @genpd: PM domain to power up.
865 * @depth: nesting count for lockdep.
867 * Restore power to @genpd and all of its parents so that it is possible to
868 * resume a device belonging to it.
870 static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
872 struct gpd_link *link;
875 if (genpd_status_on(genpd))
879 * The list is guaranteed not to change while the loop below is being
880 * executed, unless one of the parents' .power_on() callbacks fiddles
883 list_for_each_entry(link, &genpd->child_links, child_node) {
884 struct generic_pm_domain *parent = link->parent;
886 genpd_sd_counter_inc(parent);
888 genpd_lock_nested(parent, depth + 1);
889 ret = genpd_power_on(parent, depth + 1);
890 genpd_unlock(parent);
893 genpd_sd_counter_dec(parent);
898 ret = _genpd_power_on(genpd, true);
902 genpd->status = GENPD_STATE_ON;
903 genpd_update_accounting(genpd);
908 list_for_each_entry_continue_reverse(link,
911 genpd_sd_counter_dec(link->parent);
912 genpd_lock_nested(link->parent, depth + 1);
913 genpd_power_off(link->parent, false, depth + 1);
914 genpd_unlock(link->parent);
920 static int genpd_dev_pm_start(struct device *dev)
922 struct generic_pm_domain *genpd = dev_to_genpd(dev);
924 return genpd_start_dev(genpd, dev);
927 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
928 unsigned long val, void *ptr)
930 struct generic_pm_domain_data *gpd_data;
933 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
934 dev = gpd_data->base.dev;
937 struct generic_pm_domain *genpd = ERR_PTR(-ENODATA);
938 struct pm_domain_data *pdd;
939 struct gpd_timing_data *td;
941 spin_lock_irq(&dev->power.lock);
943 pdd = dev->power.subsys_data ?
944 dev->power.subsys_data->domain_data : NULL;
946 td = to_gpd_data(pdd)->td;
948 td->constraint_changed = true;
949 genpd = dev_to_genpd(dev);
953 spin_unlock_irq(&dev->power.lock);
955 if (!IS_ERR(genpd)) {
957 genpd->gd->max_off_time_changed = true;
962 if (!dev || dev->power.ignore_children)
970 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
971 * @work: Work structure used for scheduling the execution of this function.
973 static void genpd_power_off_work_fn(struct work_struct *work)
975 struct generic_pm_domain *genpd;
977 genpd = container_of(work, struct generic_pm_domain, power_off_work);
980 genpd_power_off(genpd, false, 0);
985 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
986 * @dev: Device to handle.
988 static int __genpd_runtime_suspend(struct device *dev)
990 int (*cb)(struct device *__dev);
992 if (dev->type && dev->type->pm)
993 cb = dev->type->pm->runtime_suspend;
994 else if (dev->class && dev->class->pm)
995 cb = dev->class->pm->runtime_suspend;
996 else if (dev->bus && dev->bus->pm)
997 cb = dev->bus->pm->runtime_suspend;
1001 if (!cb && dev->driver && dev->driver->pm)
1002 cb = dev->driver->pm->runtime_suspend;
1004 return cb ? cb(dev) : 0;
1008 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
1009 * @dev: Device to handle.
1011 static int __genpd_runtime_resume(struct device *dev)
1013 int (*cb)(struct device *__dev);
1015 if (dev->type && dev->type->pm)
1016 cb = dev->type->pm->runtime_resume;
1017 else if (dev->class && dev->class->pm)
1018 cb = dev->class->pm->runtime_resume;
1019 else if (dev->bus && dev->bus->pm)
1020 cb = dev->bus->pm->runtime_resume;
1024 if (!cb && dev->driver && dev->driver->pm)
1025 cb = dev->driver->pm->runtime_resume;
1027 return cb ? cb(dev) : 0;
1031 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
1032 * @dev: Device to suspend.
1034 * Carry out a runtime suspend of a device under the assumption that its
1035 * pm_domain field points to the domain member of an object of type
1036 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
1038 static int genpd_runtime_suspend(struct device *dev)
1040 struct generic_pm_domain *genpd;
1041 bool (*suspend_ok)(struct device *__dev);
1042 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
1043 struct gpd_timing_data *td = gpd_data->td;
1044 bool runtime_pm = pm_runtime_enabled(dev);
1045 ktime_t time_start = 0;
1049 dev_dbg(dev, "%s()\n", __func__);
1051 genpd = dev_to_genpd(dev);
1056 * A runtime PM centric subsystem/driver may re-use the runtime PM
1057 * callbacks for other purposes than runtime PM. In those scenarios
1058 * runtime PM is disabled. Under these circumstances, we shall skip
1059 * validating/measuring the PM QoS latency.
1061 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
1062 if (runtime_pm && suspend_ok && !suspend_ok(dev))
1065 /* Measure suspend latency. */
1066 if (td && runtime_pm)
1067 time_start = ktime_get();
1069 ret = __genpd_runtime_suspend(dev);
1073 ret = genpd_stop_dev(genpd, dev);
1075 __genpd_runtime_resume(dev);
1079 /* Update suspend latency value if the measured time exceeds it. */
1080 if (td && runtime_pm) {
1081 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
1082 if (elapsed_ns > td->suspend_latency_ns) {
1083 td->suspend_latency_ns = elapsed_ns;
1084 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
1086 genpd->gd->max_off_time_changed = true;
1087 td->constraint_changed = true;
1092 * If power.irq_safe is set, this routine may be run with
1093 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
1095 if (irq_safe_dev_in_sleep_domain(dev, genpd))
1099 genpd_power_off(genpd, true, 0);
1100 gpd_data->rpm_pstate = genpd_drop_performance_state(dev);
1101 genpd_unlock(genpd);
1107 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
1108 * @dev: Device to resume.
1110 * Carry out a runtime resume of a device under the assumption that its
1111 * pm_domain field points to the domain member of an object of type
1112 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
1114 static int genpd_runtime_resume(struct device *dev)
1116 struct generic_pm_domain *genpd;
1117 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
1118 struct gpd_timing_data *td = gpd_data->td;
1119 bool timed = td && pm_runtime_enabled(dev);
1120 ktime_t time_start = 0;
1124 dev_dbg(dev, "%s()\n", __func__);
1126 genpd = dev_to_genpd(dev);
1131 * As we don't power off a non IRQ safe domain, which holds
1132 * an IRQ safe device, we don't need to restore power to it.
1134 if (irq_safe_dev_in_sleep_domain(dev, genpd))
1138 genpd_restore_performance_state(dev, gpd_data->rpm_pstate);
1139 ret = genpd_power_on(genpd, 0);
1140 genpd_unlock(genpd);
1146 /* Measure resume latency. */
1148 time_start = ktime_get();
1150 ret = genpd_start_dev(genpd, dev);
1154 ret = __genpd_runtime_resume(dev);
1158 /* Update resume latency value if the measured time exceeds it. */
1160 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
1161 if (elapsed_ns > td->resume_latency_ns) {
1162 td->resume_latency_ns = elapsed_ns;
1163 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
1165 genpd->gd->max_off_time_changed = true;
1166 td->constraint_changed = true;
1173 genpd_stop_dev(genpd, dev);
1175 if (!pm_runtime_is_irq_safe(dev) || genpd_is_irq_safe(genpd)) {
1177 genpd_power_off(genpd, true, 0);
1178 gpd_data->rpm_pstate = genpd_drop_performance_state(dev);
1179 genpd_unlock(genpd);
1185 static bool pd_ignore_unused;
1186 static int __init pd_ignore_unused_setup(char *__unused)
1188 pd_ignore_unused = true;
1191 __setup("pd_ignore_unused", pd_ignore_unused_setup);
1194 * genpd_power_off_unused - Power off all PM domains with no devices in use.
1196 static int __init genpd_power_off_unused(void)
1198 struct generic_pm_domain *genpd;
1200 if (pd_ignore_unused) {
1201 pr_warn("genpd: Not disabling unused power domains\n");
1205 pr_info("genpd: Disabling unused power domains\n");
1206 mutex_lock(&gpd_list_lock);
1208 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
1209 genpd_queue_power_off_work(genpd);
1211 mutex_unlock(&gpd_list_lock);
1215 late_initcall_sync(genpd_power_off_unused);
1217 #ifdef CONFIG_PM_SLEEP
1220 * genpd_sync_power_off - Synchronously power off a PM domain and its parents.
1221 * @genpd: PM domain to power off, if possible.
1222 * @use_lock: use the lock.
1223 * @depth: nesting count for lockdep.
1225 * Check if the given PM domain can be powered off (during system suspend or
1226 * hibernation) and do that if so. Also, in that case propagate to its parents.
1228 * This function is only called in "noirq" and "syscore" stages of system power
1229 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
1230 * these cases the lock must be held.
1232 static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
1235 struct gpd_link *link;
1237 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
1240 if (genpd->suspended_count != genpd->device_count
1241 || atomic_read(&genpd->sd_count) > 0)
1244 /* Check that the children are in their deepest (powered-off) state. */
1245 list_for_each_entry(link, &genpd->parent_links, parent_node) {
1246 struct generic_pm_domain *child = link->child;
1247 if (child->state_idx < child->state_count - 1)
1251 /* Choose the deepest state when suspending */
1252 genpd->state_idx = genpd->state_count - 1;
1253 if (_genpd_power_off(genpd, false)) {
1254 genpd->states[genpd->state_idx].rejected++;
1257 genpd->states[genpd->state_idx].usage++;
1260 genpd->status = GENPD_STATE_OFF;
1262 list_for_each_entry(link, &genpd->child_links, child_node) {
1263 genpd_sd_counter_dec(link->parent);
1266 genpd_lock_nested(link->parent, depth + 1);
1268 genpd_sync_power_off(link->parent, use_lock, depth + 1);
1271 genpd_unlock(link->parent);
1276 * genpd_sync_power_on - Synchronously power on a PM domain and its parents.
1277 * @genpd: PM domain to power on.
1278 * @use_lock: use the lock.
1279 * @depth: nesting count for lockdep.
1281 * This function is only called in "noirq" and "syscore" stages of system power
1282 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
1283 * these cases the lock must be held.
1285 static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
1288 struct gpd_link *link;
1290 if (genpd_status_on(genpd))
1293 list_for_each_entry(link, &genpd->child_links, child_node) {
1294 genpd_sd_counter_inc(link->parent);
1297 genpd_lock_nested(link->parent, depth + 1);
1299 genpd_sync_power_on(link->parent, use_lock, depth + 1);
1302 genpd_unlock(link->parent);
1305 _genpd_power_on(genpd, false);
1306 genpd->status = GENPD_STATE_ON;
1310 * genpd_prepare - Start power transition of a device in a PM domain.
1311 * @dev: Device to start the transition of.
1313 * Start a power transition of a device (during a system-wide power transition)
1314 * under the assumption that its pm_domain field points to the domain member of
1315 * an object of type struct generic_pm_domain representing a PM domain
1316 * consisting of I/O devices.
1318 static int genpd_prepare(struct device *dev)
1320 struct generic_pm_domain *genpd;
1323 dev_dbg(dev, "%s()\n", __func__);
1325 genpd = dev_to_genpd(dev);
1330 genpd->prepared_count++;
1331 genpd_unlock(genpd);
1333 ret = pm_generic_prepare(dev);
1337 genpd->prepared_count--;
1339 genpd_unlock(genpd);
1342 /* Never return 1, as genpd don't cope with the direct_complete path. */
1343 return ret >= 0 ? 0 : ret;
1347 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1349 * @dev: Device to suspend.
1350 * @suspend_noirq: Generic suspend_noirq callback.
1351 * @resume_noirq: Generic resume_noirq callback.
1353 * Stop the device and remove power from the domain if all devices in it have
1356 static int genpd_finish_suspend(struct device *dev,
1357 int (*suspend_noirq)(struct device *dev),
1358 int (*resume_noirq)(struct device *dev))
1360 struct generic_pm_domain *genpd;
1363 genpd = dev_to_genpd(dev);
1367 ret = suspend_noirq(dev);
1371 if (device_wakeup_path(dev) && genpd_is_active_wakeup(genpd))
1374 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1375 !pm_runtime_status_suspended(dev)) {
1376 ret = genpd_stop_dev(genpd, dev);
1384 genpd->suspended_count++;
1385 genpd_sync_power_off(genpd, true, 0);
1386 genpd_unlock(genpd);
1392 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1393 * @dev: Device to suspend.
1395 * Stop the device and remove power from the domain if all devices in it have
1398 static int genpd_suspend_noirq(struct device *dev)
1400 dev_dbg(dev, "%s()\n", __func__);
1402 return genpd_finish_suspend(dev,
1403 pm_generic_suspend_noirq,
1404 pm_generic_resume_noirq);
1408 * genpd_finish_resume - Completion of resume of device in an I/O PM domain.
1409 * @dev: Device to resume.
1410 * @resume_noirq: Generic resume_noirq callback.
1412 * Restore power to the device's PM domain, if necessary, and start the device.
1414 static int genpd_finish_resume(struct device *dev,
1415 int (*resume_noirq)(struct device *dev))
1417 struct generic_pm_domain *genpd;
1420 dev_dbg(dev, "%s()\n", __func__);
1422 genpd = dev_to_genpd(dev);
1426 if (device_wakeup_path(dev) && genpd_is_active_wakeup(genpd))
1427 return resume_noirq(dev);
1430 genpd_sync_power_on(genpd, true, 0);
1431 genpd->suspended_count--;
1432 genpd_unlock(genpd);
1434 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1435 !pm_runtime_status_suspended(dev)) {
1436 ret = genpd_start_dev(genpd, dev);
1441 return pm_generic_resume_noirq(dev);
1445 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1446 * @dev: Device to resume.
1448 * Restore power to the device's PM domain, if necessary, and start the device.
1450 static int genpd_resume_noirq(struct device *dev)
1452 dev_dbg(dev, "%s()\n", __func__);
1454 return genpd_finish_resume(dev, pm_generic_resume_noirq);
1458 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1459 * @dev: Device to freeze.
1461 * Carry out a late freeze of a device under the assumption that its
1462 * pm_domain field points to the domain member of an object of type
1463 * struct generic_pm_domain representing a power domain consisting of I/O
1466 static int genpd_freeze_noirq(struct device *dev)
1468 dev_dbg(dev, "%s()\n", __func__);
1470 return genpd_finish_suspend(dev,
1471 pm_generic_freeze_noirq,
1472 pm_generic_thaw_noirq);
1476 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1477 * @dev: Device to thaw.
1479 * Start the device, unless power has been removed from the domain already
1480 * before the system transition.
1482 static int genpd_thaw_noirq(struct device *dev)
1484 dev_dbg(dev, "%s()\n", __func__);
1486 return genpd_finish_resume(dev, pm_generic_thaw_noirq);
1490 * genpd_poweroff_noirq - Completion of hibernation of device in an
1492 * @dev: Device to poweroff.
1494 * Stop the device and remove power from the domain if all devices in it have
1497 static int genpd_poweroff_noirq(struct device *dev)
1499 dev_dbg(dev, "%s()\n", __func__);
1501 return genpd_finish_suspend(dev,
1502 pm_generic_poweroff_noirq,
1503 pm_generic_restore_noirq);
1507 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1508 * @dev: Device to resume.
1510 * Make sure the domain will be in the same power state as before the
1511 * hibernation the system is resuming from and start the device if necessary.
1513 static int genpd_restore_noirq(struct device *dev)
1515 dev_dbg(dev, "%s()\n", __func__);
1517 return genpd_finish_resume(dev, pm_generic_restore_noirq);
1521 * genpd_complete - Complete power transition of a device in a power domain.
1522 * @dev: Device to complete the transition of.
1524 * Complete a power transition of a device (during a system-wide power
1525 * transition) under the assumption that its pm_domain field points to the
1526 * domain member of an object of type struct generic_pm_domain representing
1527 * a power domain consisting of I/O devices.
1529 static void genpd_complete(struct device *dev)
1531 struct generic_pm_domain *genpd;
1533 dev_dbg(dev, "%s()\n", __func__);
1535 genpd = dev_to_genpd(dev);
1539 pm_generic_complete(dev);
1543 genpd->prepared_count--;
1544 if (!genpd->prepared_count)
1545 genpd_queue_power_off_work(genpd);
1547 genpd_unlock(genpd);
1550 static void genpd_switch_state(struct device *dev, bool suspend)
1552 struct generic_pm_domain *genpd;
1555 genpd = dev_to_genpd_safe(dev);
1559 use_lock = genpd_is_irq_safe(genpd);
1565 genpd->suspended_count++;
1566 genpd_sync_power_off(genpd, use_lock, 0);
1568 genpd_sync_power_on(genpd, use_lock, 0);
1569 genpd->suspended_count--;
1573 genpd_unlock(genpd);
1577 * dev_pm_genpd_suspend - Synchronously try to suspend the genpd for @dev
1578 * @dev: The device that is attached to the genpd, that can be suspended.
1580 * This routine should typically be called for a device that needs to be
1581 * suspended during the syscore suspend phase. It may also be called during
1582 * suspend-to-idle to suspend a corresponding CPU device that is attached to a
1585 void dev_pm_genpd_suspend(struct device *dev)
1587 genpd_switch_state(dev, true);
1589 EXPORT_SYMBOL_GPL(dev_pm_genpd_suspend);
1592 * dev_pm_genpd_resume - Synchronously try to resume the genpd for @dev
1593 * @dev: The device that is attached to the genpd, which needs to be resumed.
1595 * This routine should typically be called for a device that needs to be resumed
1596 * during the syscore resume phase. It may also be called during suspend-to-idle
1597 * to resume a corresponding CPU device that is attached to a genpd.
1599 void dev_pm_genpd_resume(struct device *dev)
1601 genpd_switch_state(dev, false);
1603 EXPORT_SYMBOL_GPL(dev_pm_genpd_resume);
1605 #else /* !CONFIG_PM_SLEEP */
1607 #define genpd_prepare NULL
1608 #define genpd_suspend_noirq NULL
1609 #define genpd_resume_noirq NULL
1610 #define genpd_freeze_noirq NULL
1611 #define genpd_thaw_noirq NULL
1612 #define genpd_poweroff_noirq NULL
1613 #define genpd_restore_noirq NULL
1614 #define genpd_complete NULL
1616 #endif /* CONFIG_PM_SLEEP */
1618 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1621 struct generic_pm_domain_data *gpd_data;
1622 struct gpd_timing_data *td;
1625 ret = dev_pm_get_subsys_data(dev);
1627 return ERR_PTR(ret);
1629 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1635 gpd_data->base.dev = dev;
1636 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1638 /* Allocate data used by a governor. */
1640 td = kzalloc(sizeof(*td), GFP_KERNEL);
1646 td->constraint_changed = true;
1647 td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
1648 td->next_wakeup = KTIME_MAX;
1652 spin_lock_irq(&dev->power.lock);
1654 if (dev->power.subsys_data->domain_data)
1657 dev->power.subsys_data->domain_data = &gpd_data->base;
1659 spin_unlock_irq(&dev->power.lock);
1667 kfree(gpd_data->td);
1670 dev_pm_put_subsys_data(dev);
1671 return ERR_PTR(ret);
1674 static void genpd_free_dev_data(struct device *dev,
1675 struct generic_pm_domain_data *gpd_data)
1677 spin_lock_irq(&dev->power.lock);
1679 dev->power.subsys_data->domain_data = NULL;
1681 spin_unlock_irq(&dev->power.lock);
1683 kfree(gpd_data->td);
1685 dev_pm_put_subsys_data(dev);
1688 static void genpd_update_cpumask(struct generic_pm_domain *genpd,
1689 int cpu, bool set, unsigned int depth)
1691 struct gpd_link *link;
1693 if (!genpd_is_cpu_domain(genpd))
1696 list_for_each_entry(link, &genpd->child_links, child_node) {
1697 struct generic_pm_domain *parent = link->parent;
1699 genpd_lock_nested(parent, depth + 1);
1700 genpd_update_cpumask(parent, cpu, set, depth + 1);
1701 genpd_unlock(parent);
1705 cpumask_set_cpu(cpu, genpd->cpus);
1707 cpumask_clear_cpu(cpu, genpd->cpus);
1710 static void genpd_set_cpumask(struct generic_pm_domain *genpd, int cpu)
1713 genpd_update_cpumask(genpd, cpu, true, 0);
1716 static void genpd_clear_cpumask(struct generic_pm_domain *genpd, int cpu)
1719 genpd_update_cpumask(genpd, cpu, false, 0);
1722 static int genpd_get_cpu(struct generic_pm_domain *genpd, struct device *dev)
1726 if (!genpd_is_cpu_domain(genpd))
1729 for_each_possible_cpu(cpu) {
1730 if (get_cpu_device(cpu) == dev)
1737 static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1738 struct device *base_dev)
1740 struct genpd_governor_data *gd = genpd->gd;
1741 struct generic_pm_domain_data *gpd_data;
1744 dev_dbg(dev, "%s()\n", __func__);
1746 gpd_data = genpd_alloc_dev_data(dev, gd);
1747 if (IS_ERR(gpd_data))
1748 return PTR_ERR(gpd_data);
1750 gpd_data->cpu = genpd_get_cpu(genpd, base_dev);
1752 gpd_data->hw_mode = genpd->get_hwmode_dev ? genpd->get_hwmode_dev(genpd, dev) : false;
1754 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1760 genpd_set_cpumask(genpd, gpd_data->cpu);
1761 dev_pm_domain_set(dev, &genpd->domain);
1763 genpd->device_count++;
1765 gd->max_off_time_changed = true;
1767 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1769 genpd_unlock(genpd);
1772 genpd_free_dev_data(dev, gpd_data);
1774 dev_pm_qos_add_notifier(dev, &gpd_data->nb,
1775 DEV_PM_QOS_RESUME_LATENCY);
1781 * pm_genpd_add_device - Add a device to an I/O PM domain.
1782 * @genpd: PM domain to add the device to.
1783 * @dev: Device to be added.
1785 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1792 mutex_lock(&gpd_list_lock);
1793 ret = genpd_add_device(genpd, dev, dev);
1794 mutex_unlock(&gpd_list_lock);
1798 EXPORT_SYMBOL_GPL(pm_genpd_add_device);
1800 static int genpd_remove_device(struct generic_pm_domain *genpd,
1803 struct generic_pm_domain_data *gpd_data;
1804 struct pm_domain_data *pdd;
1807 dev_dbg(dev, "%s()\n", __func__);
1809 pdd = dev->power.subsys_data->domain_data;
1810 gpd_data = to_gpd_data(pdd);
1811 dev_pm_qos_remove_notifier(dev, &gpd_data->nb,
1812 DEV_PM_QOS_RESUME_LATENCY);
1816 if (genpd->prepared_count > 0) {
1821 genpd->device_count--;
1823 genpd->gd->max_off_time_changed = true;
1825 genpd_clear_cpumask(genpd, gpd_data->cpu);
1826 dev_pm_domain_set(dev, NULL);
1828 list_del_init(&pdd->list_node);
1830 genpd_unlock(genpd);
1832 if (genpd->detach_dev)
1833 genpd->detach_dev(genpd, dev);
1835 genpd_free_dev_data(dev, gpd_data);
1840 genpd_unlock(genpd);
1841 dev_pm_qos_add_notifier(dev, &gpd_data->nb, DEV_PM_QOS_RESUME_LATENCY);
1847 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1848 * @dev: Device to be removed.
1850 int pm_genpd_remove_device(struct device *dev)
1852 struct generic_pm_domain *genpd = dev_to_genpd_safe(dev);
1857 return genpd_remove_device(genpd, dev);
1859 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1862 * dev_pm_genpd_add_notifier - Add a genpd power on/off notifier for @dev
1864 * @dev: Device that should be associated with the notifier
1865 * @nb: The notifier block to register
1867 * Users may call this function to add a genpd power on/off notifier for an
1868 * attached @dev. Only one notifier per device is allowed. The notifier is
1869 * sent when genpd is powering on/off the PM domain.
1871 * It is assumed that the user guarantee that the genpd wouldn't be detached
1872 * while this routine is getting called.
1874 * Returns 0 on success and negative error values on failures.
1876 int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb)
1878 struct generic_pm_domain *genpd;
1879 struct generic_pm_domain_data *gpd_data;
1882 genpd = dev_to_genpd_safe(dev);
1886 if (WARN_ON(!dev->power.subsys_data ||
1887 !dev->power.subsys_data->domain_data))
1890 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1891 if (gpd_data->power_nb)
1895 ret = raw_notifier_chain_register(&genpd->power_notifiers, nb);
1896 genpd_unlock(genpd);
1899 dev_warn(dev, "failed to add notifier for PM domain %s\n",
1904 gpd_data->power_nb = nb;
1907 EXPORT_SYMBOL_GPL(dev_pm_genpd_add_notifier);
1910 * dev_pm_genpd_remove_notifier - Remove a genpd power on/off notifier for @dev
1912 * @dev: Device that is associated with the notifier
1914 * Users may call this function to remove a genpd power on/off notifier for an
1917 * It is assumed that the user guarantee that the genpd wouldn't be detached
1918 * while this routine is getting called.
1920 * Returns 0 on success and negative error values on failures.
1922 int dev_pm_genpd_remove_notifier(struct device *dev)
1924 struct generic_pm_domain *genpd;
1925 struct generic_pm_domain_data *gpd_data;
1928 genpd = dev_to_genpd_safe(dev);
1932 if (WARN_ON(!dev->power.subsys_data ||
1933 !dev->power.subsys_data->domain_data))
1936 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1937 if (!gpd_data->power_nb)
1941 ret = raw_notifier_chain_unregister(&genpd->power_notifiers,
1942 gpd_data->power_nb);
1943 genpd_unlock(genpd);
1946 dev_warn(dev, "failed to remove notifier for PM domain %s\n",
1951 gpd_data->power_nb = NULL;
1954 EXPORT_SYMBOL_GPL(dev_pm_genpd_remove_notifier);
1956 static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1957 struct generic_pm_domain *subdomain)
1959 struct gpd_link *link, *itr;
1962 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1963 || genpd == subdomain)
1967 * If the domain can be powered on/off in an IRQ safe
1968 * context, ensure that the subdomain can also be
1969 * powered on/off in that context.
1971 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1972 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1973 genpd->name, subdomain->name);
1977 link = kzalloc(sizeof(*link), GFP_KERNEL);
1981 genpd_lock(subdomain);
1982 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1984 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1989 list_for_each_entry(itr, &genpd->parent_links, parent_node) {
1990 if (itr->child == subdomain && itr->parent == genpd) {
1996 link->parent = genpd;
1997 list_add_tail(&link->parent_node, &genpd->parent_links);
1998 link->child = subdomain;
1999 list_add_tail(&link->child_node, &subdomain->child_links);
2000 if (genpd_status_on(subdomain))
2001 genpd_sd_counter_inc(genpd);
2004 genpd_unlock(genpd);
2005 genpd_unlock(subdomain);
2012 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2013 * @genpd: Leader PM domain to add the subdomain to.
2014 * @subdomain: Subdomain to be added.
2016 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
2017 struct generic_pm_domain *subdomain)
2021 mutex_lock(&gpd_list_lock);
2022 ret = genpd_add_subdomain(genpd, subdomain);
2023 mutex_unlock(&gpd_list_lock);
2027 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
2030 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
2031 * @genpd: Leader PM domain to remove the subdomain from.
2032 * @subdomain: Subdomain to be removed.
2034 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
2035 struct generic_pm_domain *subdomain)
2037 struct gpd_link *l, *link;
2040 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
2043 genpd_lock(subdomain);
2044 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
2046 if (!list_empty(&subdomain->parent_links) || subdomain->device_count) {
2047 pr_warn("%s: unable to remove subdomain %s\n",
2048 genpd->name, subdomain->name);
2053 list_for_each_entry_safe(link, l, &genpd->parent_links, parent_node) {
2054 if (link->child != subdomain)
2057 list_del(&link->parent_node);
2058 list_del(&link->child_node);
2060 if (genpd_status_on(subdomain))
2061 genpd_sd_counter_dec(genpd);
2068 genpd_unlock(genpd);
2069 genpd_unlock(subdomain);
2073 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
2075 static void genpd_free_default_power_state(struct genpd_power_state *states,
2076 unsigned int state_count)
2081 static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
2083 struct genpd_power_state *state;
2085 state = kzalloc(sizeof(*state), GFP_KERNEL);
2089 genpd->states = state;
2090 genpd->state_count = 1;
2091 genpd->free_states = genpd_free_default_power_state;
2096 static int genpd_alloc_data(struct generic_pm_domain *genpd)
2098 struct genpd_governor_data *gd = NULL;
2101 if (genpd_is_cpu_domain(genpd) &&
2102 !zalloc_cpumask_var(&genpd->cpus, GFP_KERNEL))
2106 gd = kzalloc(sizeof(*gd), GFP_KERNEL);
2112 gd->max_off_time_ns = -1;
2113 gd->max_off_time_changed = true;
2114 gd->next_wakeup = KTIME_MAX;
2115 gd->next_hrtimer = KTIME_MAX;
2118 /* Use only one "off" state if there were no states declared */
2119 if (genpd->state_count == 0) {
2120 ret = genpd_set_default_power_state(genpd);
2129 if (genpd_is_cpu_domain(genpd))
2130 free_cpumask_var(genpd->cpus);
2135 static void genpd_free_data(struct generic_pm_domain *genpd)
2137 if (genpd_is_cpu_domain(genpd))
2138 free_cpumask_var(genpd->cpus);
2139 if (genpd->free_states)
2140 genpd->free_states(genpd->states, genpd->state_count);
2144 static void genpd_lock_init(struct generic_pm_domain *genpd)
2146 if (genpd_is_irq_safe(genpd)) {
2147 spin_lock_init(&genpd->slock);
2148 genpd->lock_ops = &genpd_spin_ops;
2150 mutex_init(&genpd->mlock);
2151 genpd->lock_ops = &genpd_mtx_ops;
2156 * pm_genpd_init - Initialize a generic I/O PM domain object.
2157 * @genpd: PM domain object to initialize.
2158 * @gov: PM domain governor to associate with the domain (may be NULL).
2159 * @is_off: Initial value of the domain's power_is_off field.
2161 * Returns 0 on successful initialization, else a negative error code.
2163 int pm_genpd_init(struct generic_pm_domain *genpd,
2164 struct dev_power_governor *gov, bool is_off)
2168 if (IS_ERR_OR_NULL(genpd))
2171 INIT_LIST_HEAD(&genpd->parent_links);
2172 INIT_LIST_HEAD(&genpd->child_links);
2173 INIT_LIST_HEAD(&genpd->dev_list);
2174 RAW_INIT_NOTIFIER_HEAD(&genpd->power_notifiers);
2175 genpd_lock_init(genpd);
2177 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
2178 atomic_set(&genpd->sd_count, 0);
2179 genpd->status = is_off ? GENPD_STATE_OFF : GENPD_STATE_ON;
2180 genpd->device_count = 0;
2181 genpd->provider = NULL;
2182 genpd->has_provider = false;
2183 genpd->accounting_time = ktime_get_mono_fast_ns();
2184 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
2185 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
2186 genpd->domain.ops.prepare = genpd_prepare;
2187 genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
2188 genpd->domain.ops.resume_noirq = genpd_resume_noirq;
2189 genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
2190 genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
2191 genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
2192 genpd->domain.ops.restore_noirq = genpd_restore_noirq;
2193 genpd->domain.ops.complete = genpd_complete;
2194 genpd->domain.start = genpd_dev_pm_start;
2195 genpd->domain.set_performance_state = genpd_dev_pm_set_performance_state;
2197 if (genpd->flags & GENPD_FLAG_PM_CLK) {
2198 genpd->dev_ops.stop = pm_clk_suspend;
2199 genpd->dev_ops.start = pm_clk_resume;
2202 /* The always-on governor works better with the corresponding flag. */
2203 if (gov == &pm_domain_always_on_gov)
2204 genpd->flags |= GENPD_FLAG_RPM_ALWAYS_ON;
2206 /* Always-on domains must be powered on at initialization. */
2207 if ((genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd)) &&
2208 !genpd_status_on(genpd)) {
2209 pr_err("always-on PM domain %s is not on\n", genpd->name);
2213 /* Multiple states but no governor doesn't make sense. */
2214 if (!gov && genpd->state_count > 1)
2215 pr_warn("%s: no governor for states\n", genpd->name);
2217 ret = genpd_alloc_data(genpd);
2221 device_initialize(&genpd->dev);
2222 dev_set_name(&genpd->dev, "%s", genpd->name);
2224 mutex_lock(&gpd_list_lock);
2225 list_add(&genpd->gpd_list_node, &gpd_list);
2226 mutex_unlock(&gpd_list_lock);
2227 genpd_debug_add(genpd);
2231 EXPORT_SYMBOL_GPL(pm_genpd_init);
2233 static int genpd_remove(struct generic_pm_domain *genpd)
2235 struct gpd_link *l, *link;
2237 if (IS_ERR_OR_NULL(genpd))
2242 if (genpd->has_provider) {
2243 genpd_unlock(genpd);
2244 pr_err("Provider present, unable to remove %s\n", genpd->name);
2248 if (!list_empty(&genpd->parent_links) || genpd->device_count) {
2249 genpd_unlock(genpd);
2250 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
2254 list_for_each_entry_safe(link, l, &genpd->child_links, child_node) {
2255 list_del(&link->parent_node);
2256 list_del(&link->child_node);
2260 list_del(&genpd->gpd_list_node);
2261 genpd_unlock(genpd);
2262 genpd_debug_remove(genpd);
2263 cancel_work_sync(&genpd->power_off_work);
2264 genpd_free_data(genpd);
2266 pr_debug("%s: removed %s\n", __func__, genpd->name);
2272 * pm_genpd_remove - Remove a generic I/O PM domain
2273 * @genpd: Pointer to PM domain that is to be removed.
2275 * To remove the PM domain, this function:
2276 * - Removes the PM domain as a subdomain to any parent domains,
2278 * - Removes the PM domain from the list of registered PM domains.
2280 * The PM domain will only be removed, if the associated provider has
2281 * been removed, it is not a parent to any other PM domain and has no
2282 * devices associated with it.
2284 int pm_genpd_remove(struct generic_pm_domain *genpd)
2288 mutex_lock(&gpd_list_lock);
2289 ret = genpd_remove(genpd);
2290 mutex_unlock(&gpd_list_lock);
2294 EXPORT_SYMBOL_GPL(pm_genpd_remove);
2296 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
2299 * Device Tree based PM domain providers.
2301 * The code below implements generic device tree based PM domain providers that
2302 * bind device tree nodes with generic PM domains registered in the system.
2304 * Any driver that registers generic PM domains and needs to support binding of
2305 * devices to these domains is supposed to register a PM domain provider, which
2306 * maps a PM domain specifier retrieved from the device tree to a PM domain.
2308 * Two simple mapping functions have been provided for convenience:
2309 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
2310 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
2315 * struct of_genpd_provider - PM domain provider registration structure
2316 * @link: Entry in global list of PM domain providers
2317 * @node: Pointer to device tree node of PM domain provider
2318 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
2320 * @data: context pointer to be passed into @xlate callback
2322 struct of_genpd_provider {
2323 struct list_head link;
2324 struct device_node *node;
2325 genpd_xlate_t xlate;
2329 /* List of registered PM domain providers. */
2330 static LIST_HEAD(of_genpd_providers);
2331 /* Mutex to protect the list above. */
2332 static DEFINE_MUTEX(of_genpd_mutex);
2335 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
2336 * @genpdspec: OF phandle args to map into a PM domain
2337 * @data: xlate function private data - pointer to struct generic_pm_domain
2339 * This is a generic xlate function that can be used to model PM domains that
2340 * have their own device tree nodes. The private data of xlate function needs
2341 * to be a valid pointer to struct generic_pm_domain.
2343 static struct generic_pm_domain *genpd_xlate_simple(
2344 const struct of_phandle_args *genpdspec,
2351 * genpd_xlate_onecell() - Xlate function using a single index.
2352 * @genpdspec: OF phandle args to map into a PM domain
2353 * @data: xlate function private data - pointer to struct genpd_onecell_data
2355 * This is a generic xlate function that can be used to model simple PM domain
2356 * controllers that have one device tree node and provide multiple PM domains.
2357 * A single cell is used as an index into an array of PM domains specified in
2358 * the genpd_onecell_data struct when registering the provider.
2360 static struct generic_pm_domain *genpd_xlate_onecell(
2361 const struct of_phandle_args *genpdspec,
2364 struct genpd_onecell_data *genpd_data = data;
2365 unsigned int idx = genpdspec->args[0];
2367 if (genpdspec->args_count != 1)
2368 return ERR_PTR(-EINVAL);
2370 if (idx >= genpd_data->num_domains) {
2371 pr_err("%s: invalid domain index %u\n", __func__, idx);
2372 return ERR_PTR(-EINVAL);
2375 if (!genpd_data->domains[idx])
2376 return ERR_PTR(-ENOENT);
2378 return genpd_data->domains[idx];
2382 * genpd_add_provider() - Register a PM domain provider for a node
2383 * @np: Device node pointer associated with the PM domain provider.
2384 * @xlate: Callback for decoding PM domain from phandle arguments.
2385 * @data: Context pointer for @xlate callback.
2387 static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
2390 struct of_genpd_provider *cp;
2392 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2396 cp->node = of_node_get(np);
2399 fwnode_dev_initialized(&np->fwnode, true);
2401 mutex_lock(&of_genpd_mutex);
2402 list_add(&cp->link, &of_genpd_providers);
2403 mutex_unlock(&of_genpd_mutex);
2404 pr_debug("Added domain provider from %pOF\n", np);
2409 static bool genpd_present(const struct generic_pm_domain *genpd)
2412 const struct generic_pm_domain *gpd;
2414 mutex_lock(&gpd_list_lock);
2415 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2421 mutex_unlock(&gpd_list_lock);
2427 * of_genpd_add_provider_simple() - Register a simple PM domain provider
2428 * @np: Device node pointer associated with the PM domain provider.
2429 * @genpd: Pointer to PM domain associated with the PM domain provider.
2431 int of_genpd_add_provider_simple(struct device_node *np,
2432 struct generic_pm_domain *genpd)
2439 if (!genpd_present(genpd))
2442 genpd->dev.of_node = np;
2444 /* Parse genpd OPP table */
2445 if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
2446 ret = dev_pm_opp_of_add_table(&genpd->dev);
2448 return dev_err_probe(&genpd->dev, ret, "Failed to add OPP table\n");
2451 * Save table for faster processing while setting performance
2454 genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
2455 WARN_ON(IS_ERR(genpd->opp_table));
2458 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
2460 if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
2461 dev_pm_opp_put_opp_table(genpd->opp_table);
2462 dev_pm_opp_of_remove_table(&genpd->dev);
2468 genpd->provider = &np->fwnode;
2469 genpd->has_provider = true;
2473 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
2476 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
2477 * @np: Device node pointer associated with the PM domain provider.
2478 * @data: Pointer to the data associated with the PM domain provider.
2480 int of_genpd_add_provider_onecell(struct device_node *np,
2481 struct genpd_onecell_data *data)
2483 struct generic_pm_domain *genpd;
2491 data->xlate = genpd_xlate_onecell;
2493 for (i = 0; i < data->num_domains; i++) {
2494 genpd = data->domains[i];
2498 if (!genpd_present(genpd))
2501 genpd->dev.of_node = np;
2503 /* Parse genpd OPP table */
2504 if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
2505 ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
2507 dev_err_probe(&genpd->dev, ret,
2508 "Failed to add OPP table for index %d\n", i);
2513 * Save table for faster processing while setting
2514 * performance state.
2516 genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
2517 WARN_ON(IS_ERR(genpd->opp_table));
2520 genpd->provider = &np->fwnode;
2521 genpd->has_provider = true;
2524 ret = genpd_add_provider(np, data->xlate, data);
2532 genpd = data->domains[i];
2537 genpd->provider = NULL;
2538 genpd->has_provider = false;
2540 if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
2541 dev_pm_opp_put_opp_table(genpd->opp_table);
2542 dev_pm_opp_of_remove_table(&genpd->dev);
2548 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
2551 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2552 * @np: Device node pointer associated with the PM domain provider
2554 void of_genpd_del_provider(struct device_node *np)
2556 struct of_genpd_provider *cp, *tmp;
2557 struct generic_pm_domain *gpd;
2559 mutex_lock(&gpd_list_lock);
2560 mutex_lock(&of_genpd_mutex);
2561 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
2562 if (cp->node == np) {
2564 * For each PM domain associated with the
2565 * provider, set the 'has_provider' to false
2566 * so that the PM domain can be safely removed.
2568 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2569 if (gpd->provider == &np->fwnode) {
2570 gpd->has_provider = false;
2572 if (genpd_is_opp_table_fw(gpd) || !gpd->set_performance_state)
2575 dev_pm_opp_put_opp_table(gpd->opp_table);
2576 dev_pm_opp_of_remove_table(&gpd->dev);
2580 fwnode_dev_initialized(&cp->node->fwnode, false);
2581 list_del(&cp->link);
2582 of_node_put(cp->node);
2587 mutex_unlock(&of_genpd_mutex);
2588 mutex_unlock(&gpd_list_lock);
2590 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2593 * genpd_get_from_provider() - Look-up PM domain
2594 * @genpdspec: OF phandle args to use for look-up
2596 * Looks for a PM domain provider under the node specified by @genpdspec and if
2597 * found, uses xlate function of the provider to map phandle args to a PM
2600 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2603 static struct generic_pm_domain *genpd_get_from_provider(
2604 const struct of_phandle_args *genpdspec)
2606 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2607 struct of_genpd_provider *provider;
2610 return ERR_PTR(-EINVAL);
2612 mutex_lock(&of_genpd_mutex);
2614 /* Check if we have such a provider in our array */
2615 list_for_each_entry(provider, &of_genpd_providers, link) {
2616 if (provider->node == genpdspec->np)
2617 genpd = provider->xlate(genpdspec, provider->data);
2622 mutex_unlock(&of_genpd_mutex);
2628 * of_genpd_add_device() - Add a device to an I/O PM domain
2629 * @genpdspec: OF phandle args to use for look-up PM domain
2630 * @dev: Device to be added.
2632 * Looks-up an I/O PM domain based upon phandle args provided and adds
2633 * the device to the PM domain. Returns a negative error code on failure.
2635 int of_genpd_add_device(const struct of_phandle_args *genpdspec, struct device *dev)
2637 struct generic_pm_domain *genpd;
2643 mutex_lock(&gpd_list_lock);
2645 genpd = genpd_get_from_provider(genpdspec);
2646 if (IS_ERR(genpd)) {
2647 ret = PTR_ERR(genpd);
2651 ret = genpd_add_device(genpd, dev, dev);
2654 mutex_unlock(&gpd_list_lock);
2658 EXPORT_SYMBOL_GPL(of_genpd_add_device);
2661 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2662 * @parent_spec: OF phandle args to use for parent PM domain look-up
2663 * @subdomain_spec: OF phandle args to use for subdomain look-up
2665 * Looks-up a parent PM domain and subdomain based upon phandle args
2666 * provided and adds the subdomain to the parent PM domain. Returns a
2667 * negative error code on failure.
2669 int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
2670 const struct of_phandle_args *subdomain_spec)
2672 struct generic_pm_domain *parent, *subdomain;
2675 mutex_lock(&gpd_list_lock);
2677 parent = genpd_get_from_provider(parent_spec);
2678 if (IS_ERR(parent)) {
2679 ret = PTR_ERR(parent);
2683 subdomain = genpd_get_from_provider(subdomain_spec);
2684 if (IS_ERR(subdomain)) {
2685 ret = PTR_ERR(subdomain);
2689 ret = genpd_add_subdomain(parent, subdomain);
2692 mutex_unlock(&gpd_list_lock);
2694 return ret == -ENOENT ? -EPROBE_DEFER : ret;
2696 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2699 * of_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
2700 * @parent_spec: OF phandle args to use for parent PM domain look-up
2701 * @subdomain_spec: OF phandle args to use for subdomain look-up
2703 * Looks-up a parent PM domain and subdomain based upon phandle args
2704 * provided and removes the subdomain from the parent PM domain. Returns a
2705 * negative error code on failure.
2707 int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
2708 const struct of_phandle_args *subdomain_spec)
2710 struct generic_pm_domain *parent, *subdomain;
2713 mutex_lock(&gpd_list_lock);
2715 parent = genpd_get_from_provider(parent_spec);
2716 if (IS_ERR(parent)) {
2717 ret = PTR_ERR(parent);
2721 subdomain = genpd_get_from_provider(subdomain_spec);
2722 if (IS_ERR(subdomain)) {
2723 ret = PTR_ERR(subdomain);
2727 ret = pm_genpd_remove_subdomain(parent, subdomain);
2730 mutex_unlock(&gpd_list_lock);
2734 EXPORT_SYMBOL_GPL(of_genpd_remove_subdomain);
2737 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2738 * @np: Pointer to device node associated with provider
2740 * Find the last PM domain that was added by a particular provider and
2741 * remove this PM domain from the list of PM domains. The provider is
2742 * identified by the 'provider' device structure that is passed. The PM
2743 * domain will only be removed, if the provider associated with domain
2746 * Returns a valid pointer to struct generic_pm_domain on success or
2747 * ERR_PTR() on failure.
2749 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2751 struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2754 if (IS_ERR_OR_NULL(np))
2755 return ERR_PTR(-EINVAL);
2757 mutex_lock(&gpd_list_lock);
2758 list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2759 if (gpd->provider == &np->fwnode) {
2760 ret = genpd_remove(gpd);
2761 genpd = ret ? ERR_PTR(ret) : gpd;
2765 mutex_unlock(&gpd_list_lock);
2769 EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2771 static void genpd_release_dev(struct device *dev)
2773 of_node_put(dev->of_node);
2777 static const struct bus_type genpd_bus_type = {
2782 * genpd_dev_pm_detach - Detach a device from its PM domain.
2783 * @dev: Device to detach.
2784 * @power_off: Currently not used
2786 * Try to locate a corresponding generic PM domain, which the device was
2787 * attached to previously. If such is found, the device is detached from it.
2789 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2791 struct generic_pm_domain *pd;
2795 pd = dev_to_genpd(dev);
2799 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2801 /* Drop the default performance state */
2802 if (dev_gpd_data(dev)->default_pstate) {
2803 dev_pm_genpd_set_performance_state(dev, 0);
2804 dev_gpd_data(dev)->default_pstate = 0;
2807 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2808 ret = genpd_remove_device(pd, dev);
2817 dev_err(dev, "failed to remove from PM domain %s: %d",
2822 /* Check if PM domain can be powered off after removing this device. */
2823 genpd_queue_power_off_work(pd);
2825 /* Unregister the device if it was created by genpd. */
2826 if (dev->bus == &genpd_bus_type)
2827 device_unregister(dev);
2830 static void genpd_dev_pm_sync(struct device *dev)
2832 struct generic_pm_domain *pd;
2834 pd = dev_to_genpd(dev);
2838 genpd_queue_power_off_work(pd);
2841 static int __genpd_dev_pm_attach(struct device *dev, struct device *base_dev,
2842 unsigned int index, bool power_on)
2844 struct of_phandle_args pd_args;
2845 struct generic_pm_domain *pd;
2849 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2850 "#power-domain-cells", index, &pd_args);
2854 mutex_lock(&gpd_list_lock);
2855 pd = genpd_get_from_provider(&pd_args);
2856 of_node_put(pd_args.np);
2858 mutex_unlock(&gpd_list_lock);
2859 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2860 __func__, PTR_ERR(pd));
2861 return driver_deferred_probe_check_state(base_dev);
2864 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2866 ret = genpd_add_device(pd, dev, base_dev);
2867 mutex_unlock(&gpd_list_lock);
2870 return dev_err_probe(dev, ret, "failed to add to PM domain %s\n", pd->name);
2872 dev->pm_domain->detach = genpd_dev_pm_detach;
2873 dev->pm_domain->sync = genpd_dev_pm_sync;
2875 /* Set the default performance state */
2876 pstate = of_get_required_opp_performance_state(dev->of_node, index);
2877 if (pstate < 0 && pstate != -ENODEV && pstate != -EOPNOTSUPP) {
2880 } else if (pstate > 0) {
2881 ret = dev_pm_genpd_set_performance_state(dev, pstate);
2884 dev_gpd_data(dev)->default_pstate = pstate;
2889 ret = genpd_power_on(pd, 0);
2894 /* Drop the default performance state */
2895 if (dev_gpd_data(dev)->default_pstate) {
2896 dev_pm_genpd_set_performance_state(dev, 0);
2897 dev_gpd_data(dev)->default_pstate = 0;
2900 genpd_remove_device(pd, dev);
2901 return -EPROBE_DEFER;
2907 dev_err(dev, "failed to set required performance state for power-domain %s: %d\n",
2909 genpd_remove_device(pd, dev);
2914 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2915 * @dev: Device to attach.
2917 * Parse device's OF node to find a PM domain specifier. If such is found,
2918 * attaches the device to retrieved pm_domain ops.
2920 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2921 * PM domain or when multiple power-domains exists for it, else a negative error
2922 * code. Note that if a power-domain exists for the device, but it cannot be
2923 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2924 * not probed and to re-try again later.
2926 int genpd_dev_pm_attach(struct device *dev)
2932 * Devices with multiple PM domains must be attached separately, as we
2933 * can only attach one PM domain per device.
2935 if (of_count_phandle_with_args(dev->of_node, "power-domains",
2936 "#power-domain-cells") != 1)
2939 return __genpd_dev_pm_attach(dev, dev, 0, true);
2941 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2944 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2945 * @dev: The device used to lookup the PM domain.
2946 * @index: The index of the PM domain.
2948 * Parse device's OF node to find a PM domain specifier at the provided @index.
2949 * If such is found, creates a virtual device and attaches it to the retrieved
2950 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2951 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2953 * Returns the created virtual device if successfully attached PM domain, NULL
2954 * when the device don't need a PM domain, else an ERR_PTR() in case of
2955 * failures. If a power-domain exists for the device, but cannot be found or
2956 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2957 * is not probed and to re-try again later.
2959 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
2962 struct device *virt_dev;
2969 /* Verify that the index is within a valid range. */
2970 num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
2971 "#power-domain-cells");
2972 if (index >= num_domains)
2975 /* Allocate and register device on the genpd bus. */
2976 virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL);
2978 return ERR_PTR(-ENOMEM);
2980 dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev));
2981 virt_dev->bus = &genpd_bus_type;
2982 virt_dev->release = genpd_release_dev;
2983 virt_dev->of_node = of_node_get(dev->of_node);
2985 ret = device_register(virt_dev);
2987 put_device(virt_dev);
2988 return ERR_PTR(ret);
2991 /* Try to attach the device to the PM domain at the specified index. */
2992 ret = __genpd_dev_pm_attach(virt_dev, dev, index, false);
2994 device_unregister(virt_dev);
2995 return ret ? ERR_PTR(ret) : NULL;
2998 pm_runtime_enable(virt_dev);
2999 genpd_queue_power_off_work(dev_to_genpd(virt_dev));
3003 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
3006 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
3007 * @dev: The device used to lookup the PM domain.
3008 * @name: The name of the PM domain.
3010 * Parse device's OF node to find a PM domain specifier using the
3011 * power-domain-names DT property. For further description see
3012 * genpd_dev_pm_attach_by_id().
3014 struct device *genpd_dev_pm_attach_by_name(struct device *dev, const char *name)
3021 index = of_property_match_string(dev->of_node, "power-domain-names",
3026 return genpd_dev_pm_attach_by_id(dev, index);
3029 static const struct of_device_id idle_state_match[] = {
3030 { .compatible = "domain-idle-state", },
3034 static int genpd_parse_state(struct genpd_power_state *genpd_state,
3035 struct device_node *state_node)
3039 u32 entry_latency, exit_latency;
3041 err = of_property_read_u32(state_node, "entry-latency-us",
3044 pr_debug(" * %pOF missing entry-latency-us property\n",
3049 err = of_property_read_u32(state_node, "exit-latency-us",
3052 pr_debug(" * %pOF missing exit-latency-us property\n",
3057 err = of_property_read_u32(state_node, "min-residency-us", &residency);
3059 genpd_state->residency_ns = 1000LL * residency;
3061 genpd_state->power_on_latency_ns = 1000LL * exit_latency;
3062 genpd_state->power_off_latency_ns = 1000LL * entry_latency;
3063 genpd_state->fwnode = &state_node->fwnode;
3068 static int genpd_iterate_idle_states(struct device_node *dn,
3069 struct genpd_power_state *states)
3072 struct of_phandle_iterator it;
3073 struct device_node *np;
3076 ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
3078 return ret == -ENOENT ? 0 : ret;
3080 /* Loop over the phandles until all the requested entry is found */
3081 of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
3083 if (!of_match_node(idle_state_match, np))
3086 if (!of_device_is_available(np))
3090 ret = genpd_parse_state(&states[i], np);
3092 pr_err("Parsing idle state node %pOF failed with err %d\n",
3105 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
3107 * @dn: The genpd device node
3108 * @states: The pointer to which the state array will be saved.
3109 * @n: The count of elements in the array returned from this function.
3111 * Returns the device states parsed from the OF node. The memory for the states
3112 * is allocated by this function and is the responsibility of the caller to
3113 * free the memory after use. If any or zero compatible domain idle states is
3114 * found it returns 0 and in case of errors, a negative error code is returned.
3116 int of_genpd_parse_idle_states(struct device_node *dn,
3117 struct genpd_power_state **states, int *n)
3119 struct genpd_power_state *st;
3122 ret = genpd_iterate_idle_states(dn, NULL);
3132 st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
3136 ret = genpd_iterate_idle_states(dn, st);
3139 return ret < 0 ? ret : -EINVAL;
3147 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
3149 static int __init genpd_bus_init(void)
3151 return bus_register(&genpd_bus_type);
3153 core_initcall(genpd_bus_init);
3155 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
3158 /*** debugfs support ***/
3160 #ifdef CONFIG_DEBUG_FS
3162 * TODO: This function is a slightly modified version of rtpm_status_show
3163 * from sysfs.c, so generalize it.
3165 static void rtpm_status_str(struct seq_file *s, struct device *dev)
3167 static const char * const status_lookup[] = {
3168 [RPM_ACTIVE] = "active",
3169 [RPM_RESUMING] = "resuming",
3170 [RPM_SUSPENDED] = "suspended",
3171 [RPM_SUSPENDING] = "suspending"
3175 if (dev->power.runtime_error)
3177 else if (dev->power.disable_depth)
3179 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
3180 p = status_lookup[dev->power.runtime_status];
3184 seq_printf(s, "%-25s ", p);
3187 static void mode_status_str(struct seq_file *s, struct device *dev)
3189 struct generic_pm_domain_data *gpd_data;
3191 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
3193 seq_printf(s, "%20s", gpd_data->hw_mode ? "HW" : "SW");
3196 static void perf_status_str(struct seq_file *s, struct device *dev)
3198 struct generic_pm_domain_data *gpd_data;
3200 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
3201 seq_put_decimal_ull(s, "", gpd_data->performance_state);
3204 static int genpd_summary_one(struct seq_file *s,
3205 struct generic_pm_domain *genpd)
3207 static const char * const status_lookup[] = {
3208 [GENPD_STATE_ON] = "on",
3209 [GENPD_STATE_OFF] = "off"
3211 struct pm_domain_data *pm_data;
3212 const char *kobj_path;
3213 struct gpd_link *link;
3217 ret = genpd_lock_interruptible(genpd);
3219 return -ERESTARTSYS;
3221 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
3223 if (!genpd_status_on(genpd))
3224 snprintf(state, sizeof(state), "%s-%u",
3225 status_lookup[genpd->status], genpd->state_idx);
3227 snprintf(state, sizeof(state), "%s",
3228 status_lookup[genpd->status]);
3229 seq_printf(s, "%-30s %-50s %u", genpd->name, state, genpd->performance_state);
3232 * Modifications on the list require holding locks on both
3233 * parent and child, so we are safe.
3234 * Also genpd->name is immutable.
3236 list_for_each_entry(link, &genpd->parent_links, parent_node) {
3237 if (list_is_first(&link->parent_node, &genpd->parent_links))
3238 seq_printf(s, "\n%48s", " ");
3239 seq_printf(s, "%s", link->child->name);
3240 if (!list_is_last(&link->parent_node, &genpd->parent_links))
3244 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
3245 kobj_path = kobject_get_path(&pm_data->dev->kobj,
3246 genpd_is_irq_safe(genpd) ?
3247 GFP_ATOMIC : GFP_KERNEL);
3248 if (kobj_path == NULL)
3251 seq_printf(s, "\n %-50s ", kobj_path);
3252 rtpm_status_str(s, pm_data->dev);
3253 perf_status_str(s, pm_data->dev);
3254 mode_status_str(s, pm_data->dev);
3260 genpd_unlock(genpd);
3265 static int summary_show(struct seq_file *s, void *data)
3267 struct generic_pm_domain *genpd;
3270 seq_puts(s, "domain status children performance\n");
3271 seq_puts(s, " /device runtime status managed by\n");
3272 seq_puts(s, "------------------------------------------------------------------------------------------------------------\n");
3274 ret = mutex_lock_interruptible(&gpd_list_lock);
3276 return -ERESTARTSYS;
3278 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
3279 ret = genpd_summary_one(s, genpd);
3283 mutex_unlock(&gpd_list_lock);
3288 static int status_show(struct seq_file *s, void *data)
3290 static const char * const status_lookup[] = {
3291 [GENPD_STATE_ON] = "on",
3292 [GENPD_STATE_OFF] = "off"
3295 struct generic_pm_domain *genpd = s->private;
3298 ret = genpd_lock_interruptible(genpd);
3300 return -ERESTARTSYS;
3302 if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
3305 if (genpd->status == GENPD_STATE_OFF)
3306 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
3309 seq_printf(s, "%s\n", status_lookup[genpd->status]);
3311 genpd_unlock(genpd);
3315 static int sub_domains_show(struct seq_file *s, void *data)
3317 struct generic_pm_domain *genpd = s->private;
3318 struct gpd_link *link;
3321 ret = genpd_lock_interruptible(genpd);
3323 return -ERESTARTSYS;
3325 list_for_each_entry(link, &genpd->parent_links, parent_node)
3326 seq_printf(s, "%s\n", link->child->name);
3328 genpd_unlock(genpd);
3332 static int idle_states_show(struct seq_file *s, void *data)
3334 struct generic_pm_domain *genpd = s->private;
3335 u64 now, delta, idle_time = 0;
3339 ret = genpd_lock_interruptible(genpd);
3341 return -ERESTARTSYS;
3343 seq_puts(s, "State Time Spent(ms) Usage Rejected\n");
3345 for (i = 0; i < genpd->state_count; i++) {
3346 idle_time += genpd->states[i].idle_time;
3348 if (genpd->status == GENPD_STATE_OFF && genpd->state_idx == i) {
3349 now = ktime_get_mono_fast_ns();
3350 if (now > genpd->accounting_time) {
3351 delta = now - genpd->accounting_time;
3356 do_div(idle_time, NSEC_PER_MSEC);
3357 seq_printf(s, "S%-13i %-14llu %-14llu %llu\n", i, idle_time,
3358 genpd->states[i].usage, genpd->states[i].rejected);
3361 genpd_unlock(genpd);
3365 static int active_time_show(struct seq_file *s, void *data)
3367 struct generic_pm_domain *genpd = s->private;
3368 u64 now, on_time, delta = 0;
3371 ret = genpd_lock_interruptible(genpd);
3373 return -ERESTARTSYS;
3375 if (genpd->status == GENPD_STATE_ON) {
3376 now = ktime_get_mono_fast_ns();
3377 if (now > genpd->accounting_time)
3378 delta = now - genpd->accounting_time;
3381 on_time = genpd->on_time + delta;
3382 do_div(on_time, NSEC_PER_MSEC);
3383 seq_printf(s, "%llu ms\n", on_time);
3385 genpd_unlock(genpd);
3389 static int total_idle_time_show(struct seq_file *s, void *data)
3391 struct generic_pm_domain *genpd = s->private;
3392 u64 now, delta, total = 0;
3396 ret = genpd_lock_interruptible(genpd);
3398 return -ERESTARTSYS;
3400 for (i = 0; i < genpd->state_count; i++) {
3401 total += genpd->states[i].idle_time;
3403 if (genpd->status == GENPD_STATE_OFF && genpd->state_idx == i) {
3404 now = ktime_get_mono_fast_ns();
3405 if (now > genpd->accounting_time) {
3406 delta = now - genpd->accounting_time;
3412 do_div(total, NSEC_PER_MSEC);
3413 seq_printf(s, "%llu ms\n", total);
3415 genpd_unlock(genpd);
3420 static int devices_show(struct seq_file *s, void *data)
3422 struct generic_pm_domain *genpd = s->private;
3423 struct pm_domain_data *pm_data;
3424 const char *kobj_path;
3427 ret = genpd_lock_interruptible(genpd);
3429 return -ERESTARTSYS;
3431 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
3432 kobj_path = kobject_get_path(&pm_data->dev->kobj,
3433 genpd_is_irq_safe(genpd) ?
3434 GFP_ATOMIC : GFP_KERNEL);
3435 if (kobj_path == NULL)
3438 seq_printf(s, "%s\n", kobj_path);
3442 genpd_unlock(genpd);
3446 static int perf_state_show(struct seq_file *s, void *data)
3448 struct generic_pm_domain *genpd = s->private;
3450 if (genpd_lock_interruptible(genpd))
3451 return -ERESTARTSYS;
3453 seq_printf(s, "%u\n", genpd->performance_state);
3455 genpd_unlock(genpd);
3459 DEFINE_SHOW_ATTRIBUTE(summary);
3460 DEFINE_SHOW_ATTRIBUTE(status);
3461 DEFINE_SHOW_ATTRIBUTE(sub_domains);
3462 DEFINE_SHOW_ATTRIBUTE(idle_states);
3463 DEFINE_SHOW_ATTRIBUTE(active_time);
3464 DEFINE_SHOW_ATTRIBUTE(total_idle_time);
3465 DEFINE_SHOW_ATTRIBUTE(devices);
3466 DEFINE_SHOW_ATTRIBUTE(perf_state);
3468 static void genpd_debug_add(struct generic_pm_domain *genpd)
3472 if (!genpd_debugfs_dir)
3475 d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
3477 debugfs_create_file("current_state", 0444,
3478 d, genpd, &status_fops);
3479 debugfs_create_file("sub_domains", 0444,
3480 d, genpd, &sub_domains_fops);
3481 debugfs_create_file("idle_states", 0444,
3482 d, genpd, &idle_states_fops);
3483 debugfs_create_file("active_time", 0444,
3484 d, genpd, &active_time_fops);
3485 debugfs_create_file("total_idle_time", 0444,
3486 d, genpd, &total_idle_time_fops);
3487 debugfs_create_file("devices", 0444,
3488 d, genpd, &devices_fops);
3489 if (genpd->set_performance_state)
3490 debugfs_create_file("perf_state", 0444,
3491 d, genpd, &perf_state_fops);
3494 static int __init genpd_debug_init(void)
3496 struct generic_pm_domain *genpd;
3498 genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
3500 debugfs_create_file("pm_genpd_summary", S_IRUGO, genpd_debugfs_dir,
3501 NULL, &summary_fops);
3503 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
3504 genpd_debug_add(genpd);
3508 late_initcall(genpd_debug_init);
3510 static void __exit genpd_debug_exit(void)
3512 debugfs_remove_recursive(genpd_debugfs_dir);
3514 __exitcall(genpd_debug_exit);
3515 #endif /* CONFIG_DEBUG_FS */