Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / pmdomain / core.c
bloba6c8b85dd02478e227ac74886225a3fbad330eac
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * drivers/base/power/domain.c - Common code related to device power domains.
5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 */
7 #define pr_fmt(fmt) "PM: " fmt
9 #include <linux/delay.h>
10 #include <linux/idr.h>
11 #include <linux/kernel.h>
12 #include <linux/io.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_opp.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/pm_domain.h>
17 #include <linux/pm_qos.h>
18 #include <linux/pm_clock.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
21 #include <linux/sched.h>
22 #include <linux/suspend.h>
23 #include <linux/export.h>
24 #include <linux/cpu.h>
25 #include <linux/debugfs.h>
27 /* Provides a unique ID for each genpd device */
28 static DEFINE_IDA(genpd_ida);
30 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
32 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
33 ({ \
34 type (*__routine)(struct device *__d); \
35 type __ret = (type)0; \
37 __routine = genpd->dev_ops.callback; \
38 if (__routine) { \
39 __ret = __routine(dev); \
40 } \
41 __ret; \
44 static LIST_HEAD(gpd_list);
45 static DEFINE_MUTEX(gpd_list_lock);
47 struct genpd_lock_ops {
48 void (*lock)(struct generic_pm_domain *genpd);
49 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
50 int (*lock_interruptible)(struct generic_pm_domain *genpd);
51 void (*unlock)(struct generic_pm_domain *genpd);
54 static void genpd_lock_mtx(struct generic_pm_domain *genpd)
56 mutex_lock(&genpd->mlock);
59 static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
60 int depth)
62 mutex_lock_nested(&genpd->mlock, depth);
65 static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
67 return mutex_lock_interruptible(&genpd->mlock);
70 static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
72 return mutex_unlock(&genpd->mlock);
75 static const struct genpd_lock_ops genpd_mtx_ops = {
76 .lock = genpd_lock_mtx,
77 .lock_nested = genpd_lock_nested_mtx,
78 .lock_interruptible = genpd_lock_interruptible_mtx,
79 .unlock = genpd_unlock_mtx,
82 static void genpd_lock_spin(struct generic_pm_domain *genpd)
83 __acquires(&genpd->slock)
85 unsigned long flags;
87 spin_lock_irqsave(&genpd->slock, flags);
88 genpd->lock_flags = flags;
91 static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
92 int depth)
93 __acquires(&genpd->slock)
95 unsigned long flags;
97 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
98 genpd->lock_flags = flags;
101 static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
102 __acquires(&genpd->slock)
104 unsigned long flags;
106 spin_lock_irqsave(&genpd->slock, flags);
107 genpd->lock_flags = flags;
108 return 0;
111 static void genpd_unlock_spin(struct generic_pm_domain *genpd)
112 __releases(&genpd->slock)
114 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
117 static const struct genpd_lock_ops genpd_spin_ops = {
118 .lock = genpd_lock_spin,
119 .lock_nested = genpd_lock_nested_spin,
120 .lock_interruptible = genpd_lock_interruptible_spin,
121 .unlock = genpd_unlock_spin,
124 static void genpd_lock_raw_spin(struct generic_pm_domain *genpd)
125 __acquires(&genpd->raw_slock)
127 unsigned long flags;
129 raw_spin_lock_irqsave(&genpd->raw_slock, flags);
130 genpd->raw_lock_flags = flags;
133 static void genpd_lock_nested_raw_spin(struct generic_pm_domain *genpd,
134 int depth)
135 __acquires(&genpd->raw_slock)
137 unsigned long flags;
139 raw_spin_lock_irqsave_nested(&genpd->raw_slock, flags, depth);
140 genpd->raw_lock_flags = flags;
143 static int genpd_lock_interruptible_raw_spin(struct generic_pm_domain *genpd)
144 __acquires(&genpd->raw_slock)
146 unsigned long flags;
148 raw_spin_lock_irqsave(&genpd->raw_slock, flags);
149 genpd->raw_lock_flags = flags;
150 return 0;
153 static void genpd_unlock_raw_spin(struct generic_pm_domain *genpd)
154 __releases(&genpd->raw_slock)
156 raw_spin_unlock_irqrestore(&genpd->raw_slock, genpd->raw_lock_flags);
159 static const struct genpd_lock_ops genpd_raw_spin_ops = {
160 .lock = genpd_lock_raw_spin,
161 .lock_nested = genpd_lock_nested_raw_spin,
162 .lock_interruptible = genpd_lock_interruptible_raw_spin,
163 .unlock = genpd_unlock_raw_spin,
166 #define genpd_lock(p) p->lock_ops->lock(p)
167 #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
168 #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
169 #define genpd_unlock(p) p->lock_ops->unlock(p)
171 #define genpd_status_on(genpd) (genpd->status == GENPD_STATE_ON)
172 #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
173 #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
174 #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
175 #define genpd_is_cpu_domain(genpd) (genpd->flags & GENPD_FLAG_CPU_DOMAIN)
176 #define genpd_is_rpm_always_on(genpd) (genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
177 #define genpd_is_opp_table_fw(genpd) (genpd->flags & GENPD_FLAG_OPP_TABLE_FW)
178 #define genpd_is_dev_name_fw(genpd) (genpd->flags & GENPD_FLAG_DEV_NAME_FW)
180 static inline bool irq_safe_dev_in_sleep_domain(struct device *dev,
181 const struct generic_pm_domain *genpd)
183 bool ret;
185 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
188 * Warn once if an IRQ safe device is attached to a domain, which
189 * callbacks are allowed to sleep. This indicates a suboptimal
190 * configuration for PM, but it doesn't matter for an always on domain.
192 if (genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd))
193 return ret;
195 if (ret)
196 dev_warn_once(dev, "PM domain %s will not be powered off\n",
197 dev_name(&genpd->dev));
199 return ret;
202 static int genpd_runtime_suspend(struct device *dev);
205 * Get the generic PM domain for a particular struct device.
206 * This validates the struct device pointer, the PM domain pointer,
207 * and checks that the PM domain pointer is a real generic PM domain.
208 * Any failure results in NULL being returned.
210 static struct generic_pm_domain *dev_to_genpd_safe(struct device *dev)
212 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
213 return NULL;
215 /* A genpd's always have its ->runtime_suspend() callback assigned. */
216 if (dev->pm_domain->ops.runtime_suspend == genpd_runtime_suspend)
217 return pd_to_genpd(dev->pm_domain);
219 return NULL;
223 * This should only be used where we are certain that the pm_domain
224 * attached to the device is a genpd domain.
226 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
228 if (IS_ERR_OR_NULL(dev->pm_domain))
229 return ERR_PTR(-EINVAL);
231 return pd_to_genpd(dev->pm_domain);
234 struct device *dev_to_genpd_dev(struct device *dev)
236 struct generic_pm_domain *genpd = dev_to_genpd(dev);
238 if (IS_ERR(genpd))
239 return ERR_CAST(genpd);
241 return &genpd->dev;
244 static int genpd_stop_dev(const struct generic_pm_domain *genpd,
245 struct device *dev)
247 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
250 static int genpd_start_dev(const struct generic_pm_domain *genpd,
251 struct device *dev)
253 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
256 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
258 bool ret = false;
260 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
261 ret = !!atomic_dec_and_test(&genpd->sd_count);
263 return ret;
266 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
268 atomic_inc(&genpd->sd_count);
269 smp_mb__after_atomic();
272 #ifdef CONFIG_DEBUG_FS
273 static struct dentry *genpd_debugfs_dir;
275 static void genpd_debug_add(struct generic_pm_domain *genpd);
277 static void genpd_debug_remove(struct generic_pm_domain *genpd)
279 if (!genpd_debugfs_dir)
280 return;
282 debugfs_lookup_and_remove(dev_name(&genpd->dev), genpd_debugfs_dir);
285 static void genpd_update_accounting(struct generic_pm_domain *genpd)
287 u64 delta, now;
289 now = ktime_get_mono_fast_ns();
290 if (now <= genpd->accounting_time)
291 return;
293 delta = now - genpd->accounting_time;
296 * If genpd->status is active, it means we are just
297 * out of off and so update the idle time and vice
298 * versa.
300 if (genpd->status == GENPD_STATE_ON)
301 genpd->states[genpd->state_idx].idle_time += delta;
302 else
303 genpd->on_time += delta;
305 genpd->accounting_time = now;
307 #else
308 static inline void genpd_debug_add(struct generic_pm_domain *genpd) {}
309 static inline void genpd_debug_remove(struct generic_pm_domain *genpd) {}
310 static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
311 #endif
313 static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd,
314 unsigned int state)
316 struct generic_pm_domain_data *pd_data;
317 struct pm_domain_data *pdd;
318 struct gpd_link *link;
320 /* New requested state is same as Max requested state */
321 if (state == genpd->performance_state)
322 return state;
324 /* New requested state is higher than Max requested state */
325 if (state > genpd->performance_state)
326 return state;
328 /* Traverse all devices within the domain */
329 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
330 pd_data = to_gpd_data(pdd);
332 if (pd_data->performance_state > state)
333 state = pd_data->performance_state;
337 * Traverse all sub-domains within the domain. This can be
338 * done without any additional locking as the link->performance_state
339 * field is protected by the parent genpd->lock, which is already taken.
341 * Also note that link->performance_state (subdomain's performance state
342 * requirement to parent domain) is different from
343 * link->child->performance_state (current performance state requirement
344 * of the devices/sub-domains of the subdomain) and so can have a
345 * different value.
347 * Note that we also take vote from powered-off sub-domains into account
348 * as the same is done for devices right now.
350 list_for_each_entry(link, &genpd->parent_links, parent_node) {
351 if (link->performance_state > state)
352 state = link->performance_state;
355 return state;
358 static int genpd_xlate_performance_state(struct generic_pm_domain *genpd,
359 struct generic_pm_domain *parent,
360 unsigned int pstate)
362 if (!parent->set_performance_state)
363 return pstate;
365 return dev_pm_opp_xlate_performance_state(genpd->opp_table,
366 parent->opp_table,
367 pstate);
370 static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
371 unsigned int state, int depth);
373 static void _genpd_rollback_parent_state(struct gpd_link *link, int depth)
375 struct generic_pm_domain *parent = link->parent;
376 int parent_state;
378 genpd_lock_nested(parent, depth + 1);
380 parent_state = link->prev_performance_state;
381 link->performance_state = parent_state;
383 parent_state = _genpd_reeval_performance_state(parent, parent_state);
384 if (_genpd_set_performance_state(parent, parent_state, depth + 1)) {
385 pr_err("%s: Failed to roll back to %d performance state\n",
386 parent->name, parent_state);
389 genpd_unlock(parent);
392 static int _genpd_set_parent_state(struct generic_pm_domain *genpd,
393 struct gpd_link *link,
394 unsigned int state, int depth)
396 struct generic_pm_domain *parent = link->parent;
397 int parent_state, ret;
399 /* Find parent's performance state */
400 ret = genpd_xlate_performance_state(genpd, parent, state);
401 if (unlikely(ret < 0))
402 return ret;
404 parent_state = ret;
406 genpd_lock_nested(parent, depth + 1);
408 link->prev_performance_state = link->performance_state;
409 link->performance_state = parent_state;
411 parent_state = _genpd_reeval_performance_state(parent, parent_state);
412 ret = _genpd_set_performance_state(parent, parent_state, depth + 1);
413 if (ret)
414 link->performance_state = link->prev_performance_state;
416 genpd_unlock(parent);
418 return ret;
421 static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
422 unsigned int state, int depth)
424 struct gpd_link *link = NULL;
425 int ret;
427 if (state == genpd->performance_state)
428 return 0;
430 /* When scaling up, propagate to parents first in normal order */
431 if (state > genpd->performance_state) {
432 list_for_each_entry(link, &genpd->child_links, child_node) {
433 ret = _genpd_set_parent_state(genpd, link, state, depth);
434 if (ret)
435 goto rollback_parents_up;
439 if (genpd->set_performance_state) {
440 ret = genpd->set_performance_state(genpd, state);
441 if (ret) {
442 if (link)
443 goto rollback_parents_up;
444 return ret;
448 /* When scaling down, propagate to parents last in reverse order */
449 if (state < genpd->performance_state) {
450 list_for_each_entry_reverse(link, &genpd->child_links, child_node) {
451 ret = _genpd_set_parent_state(genpd, link, state, depth);
452 if (ret)
453 goto rollback_parents_down;
457 genpd->performance_state = state;
458 return 0;
460 rollback_parents_up:
461 list_for_each_entry_continue_reverse(link, &genpd->child_links, child_node)
462 _genpd_rollback_parent_state(link, depth);
463 return ret;
464 rollback_parents_down:
465 list_for_each_entry_continue(link, &genpd->child_links, child_node)
466 _genpd_rollback_parent_state(link, depth);
467 return ret;
470 static int genpd_set_performance_state(struct device *dev, unsigned int state)
472 struct generic_pm_domain *genpd = dev_to_genpd(dev);
473 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
474 unsigned int prev_state;
475 int ret;
477 prev_state = gpd_data->performance_state;
478 if (prev_state == state)
479 return 0;
481 gpd_data->performance_state = state;
482 state = _genpd_reeval_performance_state(genpd, state);
484 ret = _genpd_set_performance_state(genpd, state, 0);
485 if (ret)
486 gpd_data->performance_state = prev_state;
488 return ret;
491 static int genpd_drop_performance_state(struct device *dev)
493 unsigned int prev_state = dev_gpd_data(dev)->performance_state;
495 if (!genpd_set_performance_state(dev, 0))
496 return prev_state;
498 return 0;
501 static void genpd_restore_performance_state(struct device *dev,
502 unsigned int state)
504 if (state)
505 genpd_set_performance_state(dev, state);
508 static int genpd_dev_pm_set_performance_state(struct device *dev,
509 unsigned int state)
511 struct generic_pm_domain *genpd = dev_to_genpd(dev);
512 int ret = 0;
514 genpd_lock(genpd);
515 if (pm_runtime_suspended(dev)) {
516 dev_gpd_data(dev)->rpm_pstate = state;
517 } else {
518 ret = genpd_set_performance_state(dev, state);
519 if (!ret)
520 dev_gpd_data(dev)->rpm_pstate = 0;
522 genpd_unlock(genpd);
524 return ret;
528 * dev_pm_genpd_set_performance_state- Set performance state of device's power
529 * domain.
531 * @dev: Device for which the performance-state needs to be set.
532 * @state: Target performance state of the device. This can be set as 0 when the
533 * device doesn't have any performance state constraints left (And so
534 * the device wouldn't participate anymore to find the target
535 * performance state of the genpd).
537 * It is assumed that the users guarantee that the genpd wouldn't be detached
538 * while this routine is getting called.
540 * Returns 0 on success and negative error values on failures.
542 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
544 struct generic_pm_domain *genpd;
546 genpd = dev_to_genpd_safe(dev);
547 if (!genpd)
548 return -ENODEV;
550 if (WARN_ON(!dev->power.subsys_data ||
551 !dev->power.subsys_data->domain_data))
552 return -EINVAL;
554 return genpd_dev_pm_set_performance_state(dev, state);
556 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
559 * dev_pm_genpd_set_next_wakeup - Notify PM framework of an impending wakeup.
561 * @dev: Device to handle
562 * @next: impending interrupt/wakeup for the device
565 * Allow devices to inform of the next wakeup. It's assumed that the users
566 * guarantee that the genpd wouldn't be detached while this routine is getting
567 * called. Additionally, it's also assumed that @dev isn't runtime suspended
568 * (RPM_SUSPENDED)."
569 * Although devices are expected to update the next_wakeup after the end of
570 * their usecase as well, it is possible the devices themselves may not know
571 * about that, so stale @next will be ignored when powering off the domain.
573 void dev_pm_genpd_set_next_wakeup(struct device *dev, ktime_t next)
575 struct generic_pm_domain *genpd;
576 struct gpd_timing_data *td;
578 genpd = dev_to_genpd_safe(dev);
579 if (!genpd)
580 return;
582 td = to_gpd_data(dev->power.subsys_data->domain_data)->td;
583 if (td)
584 td->next_wakeup = next;
586 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_next_wakeup);
589 * dev_pm_genpd_get_next_hrtimer - Return the next_hrtimer for the genpd
590 * @dev: A device that is attached to the genpd.
592 * This routine should typically be called for a device, at the point of when a
593 * GENPD_NOTIFY_PRE_OFF notification has been sent for it.
595 * Returns the aggregated value of the genpd's next hrtimer or KTIME_MAX if no
596 * valid value have been set.
598 ktime_t dev_pm_genpd_get_next_hrtimer(struct device *dev)
600 struct generic_pm_domain *genpd;
602 genpd = dev_to_genpd_safe(dev);
603 if (!genpd)
604 return KTIME_MAX;
606 if (genpd->gd)
607 return genpd->gd->next_hrtimer;
609 return KTIME_MAX;
611 EXPORT_SYMBOL_GPL(dev_pm_genpd_get_next_hrtimer);
614 * dev_pm_genpd_synced_poweroff - Next power off should be synchronous
616 * @dev: A device that is attached to the genpd.
618 * Allows a consumer of the genpd to notify the provider that the next power off
619 * should be synchronous.
621 * It is assumed that the users guarantee that the genpd wouldn't be detached
622 * while this routine is getting called.
624 void dev_pm_genpd_synced_poweroff(struct device *dev)
626 struct generic_pm_domain *genpd;
628 genpd = dev_to_genpd_safe(dev);
629 if (!genpd)
630 return;
632 genpd_lock(genpd);
633 genpd->synced_poweroff = true;
634 genpd_unlock(genpd);
636 EXPORT_SYMBOL_GPL(dev_pm_genpd_synced_poweroff);
639 * dev_pm_genpd_set_hwmode() - Set the HW mode for the device and its PM domain.
641 * @dev: Device for which the HW-mode should be changed.
642 * @enable: Value to set or unset the HW-mode.
644 * Some PM domains can rely on HW signals to control the power for a device. To
645 * allow a consumer driver to switch the behaviour for its device in runtime,
646 * which may be beneficial from a latency or energy point of view, this function
647 * may be called.
649 * It is assumed that the users guarantee that the genpd wouldn't be detached
650 * while this routine is getting called.
652 * Return: Returns 0 on success and negative error values on failures.
654 int dev_pm_genpd_set_hwmode(struct device *dev, bool enable)
656 struct generic_pm_domain *genpd;
657 int ret = 0;
659 genpd = dev_to_genpd_safe(dev);
660 if (!genpd)
661 return -ENODEV;
663 if (!genpd->set_hwmode_dev)
664 return -EOPNOTSUPP;
666 genpd_lock(genpd);
668 if (dev_gpd_data(dev)->hw_mode == enable)
669 goto out;
671 ret = genpd->set_hwmode_dev(genpd, dev, enable);
672 if (!ret)
673 dev_gpd_data(dev)->hw_mode = enable;
675 out:
676 genpd_unlock(genpd);
677 return ret;
679 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_hwmode);
682 * dev_pm_genpd_get_hwmode() - Get the HW mode setting for the device.
684 * @dev: Device for which the current HW-mode setting should be fetched.
686 * This helper function allows consumer drivers to fetch the current HW mode
687 * setting of its the device.
689 * It is assumed that the users guarantee that the genpd wouldn't be detached
690 * while this routine is getting called.
692 * Return: Returns the HW mode setting of device from SW cached hw_mode.
694 bool dev_pm_genpd_get_hwmode(struct device *dev)
696 return dev_gpd_data(dev)->hw_mode;
698 EXPORT_SYMBOL_GPL(dev_pm_genpd_get_hwmode);
700 static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
702 unsigned int state_idx = genpd->state_idx;
703 ktime_t time_start;
704 s64 elapsed_ns;
705 int ret;
707 /* Notify consumers that we are about to power on. */
708 ret = raw_notifier_call_chain_robust(&genpd->power_notifiers,
709 GENPD_NOTIFY_PRE_ON,
710 GENPD_NOTIFY_OFF, NULL);
711 ret = notifier_to_errno(ret);
712 if (ret)
713 return ret;
715 if (!genpd->power_on)
716 goto out;
718 timed = timed && genpd->gd && !genpd->states[state_idx].fwnode;
719 if (!timed) {
720 ret = genpd->power_on(genpd);
721 if (ret)
722 goto err;
724 goto out;
727 time_start = ktime_get();
728 ret = genpd->power_on(genpd);
729 if (ret)
730 goto err;
732 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
733 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
734 goto out;
736 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
737 genpd->gd->max_off_time_changed = true;
738 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
739 dev_name(&genpd->dev), "on", elapsed_ns);
741 out:
742 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_ON, NULL);
743 genpd->synced_poweroff = false;
744 return 0;
745 err:
746 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_OFF,
747 NULL);
748 return ret;
751 static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
753 unsigned int state_idx = genpd->state_idx;
754 ktime_t time_start;
755 s64 elapsed_ns;
756 int ret;
758 /* Notify consumers that we are about to power off. */
759 ret = raw_notifier_call_chain_robust(&genpd->power_notifiers,
760 GENPD_NOTIFY_PRE_OFF,
761 GENPD_NOTIFY_ON, NULL);
762 ret = notifier_to_errno(ret);
763 if (ret)
764 return ret;
766 if (!genpd->power_off)
767 goto out;
769 timed = timed && genpd->gd && !genpd->states[state_idx].fwnode;
770 if (!timed) {
771 ret = genpd->power_off(genpd);
772 if (ret)
773 goto busy;
775 goto out;
778 time_start = ktime_get();
779 ret = genpd->power_off(genpd);
780 if (ret)
781 goto busy;
783 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
784 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
785 goto out;
787 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
788 genpd->gd->max_off_time_changed = true;
789 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
790 dev_name(&genpd->dev), "off", elapsed_ns);
792 out:
793 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_OFF,
794 NULL);
795 return 0;
796 busy:
797 raw_notifier_call_chain(&genpd->power_notifiers, GENPD_NOTIFY_ON, NULL);
798 return ret;
802 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
803 * @genpd: PM domain to power off.
805 * Queue up the execution of genpd_power_off() unless it's already been done
806 * before.
808 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
810 queue_work(pm_wq, &genpd->power_off_work);
814 * genpd_power_off - Remove power from a given PM domain.
815 * @genpd: PM domain to power down.
816 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
817 * RPM status of the releated device is in an intermediate state, not yet turned
818 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
819 * be RPM_SUSPENDED, while it tries to power off the PM domain.
820 * @depth: nesting count for lockdep.
822 * If all of the @genpd's devices have been suspended and all of its subdomains
823 * have been powered down, remove power from @genpd.
825 static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
826 unsigned int depth)
828 struct pm_domain_data *pdd;
829 struct gpd_link *link;
830 unsigned int not_suspended = 0;
831 int ret;
834 * Do not try to power off the domain in the following situations:
835 * (1) The domain is already in the "power off" state.
836 * (2) System suspend is in progress.
838 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
839 return 0;
842 * Abort power off for the PM domain in the following situations:
843 * (1) The domain is configured as always on.
844 * (2) When the domain has a subdomain being powered on.
846 if (genpd_is_always_on(genpd) ||
847 genpd_is_rpm_always_on(genpd) ||
848 atomic_read(&genpd->sd_count) > 0)
849 return -EBUSY;
852 * The children must be in their deepest (powered-off) states to allow
853 * the parent to be powered off. Note that, there's no need for
854 * additional locking, as powering on a child, requires the parent's
855 * lock to be acquired first.
857 list_for_each_entry(link, &genpd->parent_links, parent_node) {
858 struct generic_pm_domain *child = link->child;
859 if (child->state_idx < child->state_count - 1)
860 return -EBUSY;
863 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
865 * Do not allow PM domain to be powered off, when an IRQ safe
866 * device is part of a non-IRQ safe domain.
868 if (!pm_runtime_suspended(pdd->dev) ||
869 irq_safe_dev_in_sleep_domain(pdd->dev, genpd))
870 not_suspended++;
873 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
874 return -EBUSY;
876 if (genpd->gov && genpd->gov->power_down_ok) {
877 if (!genpd->gov->power_down_ok(&genpd->domain))
878 return -EAGAIN;
881 /* Default to shallowest state. */
882 if (!genpd->gov)
883 genpd->state_idx = 0;
885 /* Don't power off, if a child domain is waiting to power on. */
886 if (atomic_read(&genpd->sd_count) > 0)
887 return -EBUSY;
889 ret = _genpd_power_off(genpd, true);
890 if (ret) {
891 genpd->states[genpd->state_idx].rejected++;
892 return ret;
895 genpd->status = GENPD_STATE_OFF;
896 genpd_update_accounting(genpd);
897 genpd->states[genpd->state_idx].usage++;
899 list_for_each_entry(link, &genpd->child_links, child_node) {
900 genpd_sd_counter_dec(link->parent);
901 genpd_lock_nested(link->parent, depth + 1);
902 genpd_power_off(link->parent, false, depth + 1);
903 genpd_unlock(link->parent);
906 return 0;
910 * genpd_power_on - Restore power to a given PM domain and its parents.
911 * @genpd: PM domain to power up.
912 * @depth: nesting count for lockdep.
914 * Restore power to @genpd and all of its parents so that it is possible to
915 * resume a device belonging to it.
917 static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
919 struct gpd_link *link;
920 int ret = 0;
922 if (genpd_status_on(genpd))
923 return 0;
926 * The list is guaranteed not to change while the loop below is being
927 * executed, unless one of the parents' .power_on() callbacks fiddles
928 * with it.
930 list_for_each_entry(link, &genpd->child_links, child_node) {
931 struct generic_pm_domain *parent = link->parent;
933 genpd_sd_counter_inc(parent);
935 genpd_lock_nested(parent, depth + 1);
936 ret = genpd_power_on(parent, depth + 1);
937 genpd_unlock(parent);
939 if (ret) {
940 genpd_sd_counter_dec(parent);
941 goto err;
945 ret = _genpd_power_on(genpd, true);
946 if (ret)
947 goto err;
949 genpd->status = GENPD_STATE_ON;
950 genpd_update_accounting(genpd);
952 return 0;
954 err:
955 list_for_each_entry_continue_reverse(link,
956 &genpd->child_links,
957 child_node) {
958 genpd_sd_counter_dec(link->parent);
959 genpd_lock_nested(link->parent, depth + 1);
960 genpd_power_off(link->parent, false, depth + 1);
961 genpd_unlock(link->parent);
964 return ret;
967 static int genpd_dev_pm_start(struct device *dev)
969 struct generic_pm_domain *genpd = dev_to_genpd(dev);
971 return genpd_start_dev(genpd, dev);
974 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
975 unsigned long val, void *ptr)
977 struct generic_pm_domain_data *gpd_data;
978 struct device *dev;
980 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
981 dev = gpd_data->base.dev;
983 for (;;) {
984 struct generic_pm_domain *genpd = ERR_PTR(-ENODATA);
985 struct pm_domain_data *pdd;
986 struct gpd_timing_data *td;
988 spin_lock_irq(&dev->power.lock);
990 pdd = dev->power.subsys_data ?
991 dev->power.subsys_data->domain_data : NULL;
992 if (pdd) {
993 td = to_gpd_data(pdd)->td;
994 if (td) {
995 td->constraint_changed = true;
996 genpd = dev_to_genpd(dev);
1000 spin_unlock_irq(&dev->power.lock);
1002 if (!IS_ERR(genpd)) {
1003 genpd_lock(genpd);
1004 genpd->gd->max_off_time_changed = true;
1005 genpd_unlock(genpd);
1008 dev = dev->parent;
1009 if (!dev || dev->power.ignore_children)
1010 break;
1013 return NOTIFY_DONE;
1017 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
1018 * @work: Work structure used for scheduling the execution of this function.
1020 static void genpd_power_off_work_fn(struct work_struct *work)
1022 struct generic_pm_domain *genpd;
1024 genpd = container_of(work, struct generic_pm_domain, power_off_work);
1026 genpd_lock(genpd);
1027 genpd_power_off(genpd, false, 0);
1028 genpd_unlock(genpd);
1032 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
1033 * @dev: Device to handle.
1035 static int __genpd_runtime_suspend(struct device *dev)
1037 int (*cb)(struct device *__dev);
1039 if (dev->type && dev->type->pm)
1040 cb = dev->type->pm->runtime_suspend;
1041 else if (dev->class && dev->class->pm)
1042 cb = dev->class->pm->runtime_suspend;
1043 else if (dev->bus && dev->bus->pm)
1044 cb = dev->bus->pm->runtime_suspend;
1045 else
1046 cb = NULL;
1048 if (!cb && dev->driver && dev->driver->pm)
1049 cb = dev->driver->pm->runtime_suspend;
1051 return cb ? cb(dev) : 0;
1055 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
1056 * @dev: Device to handle.
1058 static int __genpd_runtime_resume(struct device *dev)
1060 int (*cb)(struct device *__dev);
1062 if (dev->type && dev->type->pm)
1063 cb = dev->type->pm->runtime_resume;
1064 else if (dev->class && dev->class->pm)
1065 cb = dev->class->pm->runtime_resume;
1066 else if (dev->bus && dev->bus->pm)
1067 cb = dev->bus->pm->runtime_resume;
1068 else
1069 cb = NULL;
1071 if (!cb && dev->driver && dev->driver->pm)
1072 cb = dev->driver->pm->runtime_resume;
1074 return cb ? cb(dev) : 0;
1078 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
1079 * @dev: Device to suspend.
1081 * Carry out a runtime suspend of a device under the assumption that its
1082 * pm_domain field points to the domain member of an object of type
1083 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
1085 static int genpd_runtime_suspend(struct device *dev)
1087 struct generic_pm_domain *genpd;
1088 bool (*suspend_ok)(struct device *__dev);
1089 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
1090 struct gpd_timing_data *td = gpd_data->td;
1091 bool runtime_pm = pm_runtime_enabled(dev);
1092 ktime_t time_start = 0;
1093 s64 elapsed_ns;
1094 int ret;
1096 dev_dbg(dev, "%s()\n", __func__);
1098 genpd = dev_to_genpd(dev);
1099 if (IS_ERR(genpd))
1100 return -EINVAL;
1103 * A runtime PM centric subsystem/driver may re-use the runtime PM
1104 * callbacks for other purposes than runtime PM. In those scenarios
1105 * runtime PM is disabled. Under these circumstances, we shall skip
1106 * validating/measuring the PM QoS latency.
1108 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
1109 if (runtime_pm && suspend_ok && !suspend_ok(dev))
1110 return -EBUSY;
1112 /* Measure suspend latency. */
1113 if (td && runtime_pm)
1114 time_start = ktime_get();
1116 ret = __genpd_runtime_suspend(dev);
1117 if (ret)
1118 return ret;
1120 ret = genpd_stop_dev(genpd, dev);
1121 if (ret) {
1122 __genpd_runtime_resume(dev);
1123 return ret;
1126 /* Update suspend latency value if the measured time exceeds it. */
1127 if (td && runtime_pm) {
1128 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
1129 if (elapsed_ns > td->suspend_latency_ns) {
1130 td->suspend_latency_ns = elapsed_ns;
1131 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
1132 elapsed_ns);
1133 genpd->gd->max_off_time_changed = true;
1134 td->constraint_changed = true;
1139 * If power.irq_safe is set, this routine may be run with
1140 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
1142 if (irq_safe_dev_in_sleep_domain(dev, genpd))
1143 return 0;
1145 genpd_lock(genpd);
1146 genpd_power_off(genpd, true, 0);
1147 gpd_data->rpm_pstate = genpd_drop_performance_state(dev);
1148 genpd_unlock(genpd);
1150 return 0;
1154 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
1155 * @dev: Device to resume.
1157 * Carry out a runtime resume of a device under the assumption that its
1158 * pm_domain field points to the domain member of an object of type
1159 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
1161 static int genpd_runtime_resume(struct device *dev)
1163 struct generic_pm_domain *genpd;
1164 struct generic_pm_domain_data *gpd_data = dev_gpd_data(dev);
1165 struct gpd_timing_data *td = gpd_data->td;
1166 bool timed = td && pm_runtime_enabled(dev);
1167 ktime_t time_start = 0;
1168 s64 elapsed_ns;
1169 int ret;
1171 dev_dbg(dev, "%s()\n", __func__);
1173 genpd = dev_to_genpd(dev);
1174 if (IS_ERR(genpd))
1175 return -EINVAL;
1178 * As we don't power off a non IRQ safe domain, which holds
1179 * an IRQ safe device, we don't need to restore power to it.
1181 if (irq_safe_dev_in_sleep_domain(dev, genpd))
1182 goto out;
1184 genpd_lock(genpd);
1185 genpd_restore_performance_state(dev, gpd_data->rpm_pstate);
1186 ret = genpd_power_on(genpd, 0);
1187 genpd_unlock(genpd);
1189 if (ret)
1190 return ret;
1192 out:
1193 /* Measure resume latency. */
1194 if (timed)
1195 time_start = ktime_get();
1197 ret = genpd_start_dev(genpd, dev);
1198 if (ret)
1199 goto err_poweroff;
1201 ret = __genpd_runtime_resume(dev);
1202 if (ret)
1203 goto err_stop;
1205 /* Update resume latency value if the measured time exceeds it. */
1206 if (timed) {
1207 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
1208 if (elapsed_ns > td->resume_latency_ns) {
1209 td->resume_latency_ns = elapsed_ns;
1210 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
1211 elapsed_ns);
1212 genpd->gd->max_off_time_changed = true;
1213 td->constraint_changed = true;
1217 return 0;
1219 err_stop:
1220 genpd_stop_dev(genpd, dev);
1221 err_poweroff:
1222 if (!pm_runtime_is_irq_safe(dev) || genpd_is_irq_safe(genpd)) {
1223 genpd_lock(genpd);
1224 genpd_power_off(genpd, true, 0);
1225 gpd_data->rpm_pstate = genpd_drop_performance_state(dev);
1226 genpd_unlock(genpd);
1229 return ret;
1232 static bool pd_ignore_unused;
1233 static int __init pd_ignore_unused_setup(char *__unused)
1235 pd_ignore_unused = true;
1236 return 1;
1238 __setup("pd_ignore_unused", pd_ignore_unused_setup);
1241 * genpd_power_off_unused - Power off all PM domains with no devices in use.
1243 static int __init genpd_power_off_unused(void)
1245 struct generic_pm_domain *genpd;
1247 if (pd_ignore_unused) {
1248 pr_warn("genpd: Not disabling unused power domains\n");
1249 return 0;
1252 pr_info("genpd: Disabling unused power domains\n");
1253 mutex_lock(&gpd_list_lock);
1255 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
1256 genpd_queue_power_off_work(genpd);
1258 mutex_unlock(&gpd_list_lock);
1260 return 0;
1262 late_initcall_sync(genpd_power_off_unused);
1264 #ifdef CONFIG_PM_SLEEP
1267 * genpd_sync_power_off - Synchronously power off a PM domain and its parents.
1268 * @genpd: PM domain to power off, if possible.
1269 * @use_lock: use the lock.
1270 * @depth: nesting count for lockdep.
1272 * Check if the given PM domain can be powered off (during system suspend or
1273 * hibernation) and do that if so. Also, in that case propagate to its parents.
1275 * This function is only called in "noirq" and "syscore" stages of system power
1276 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
1277 * these cases the lock must be held.
1279 static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
1280 unsigned int depth)
1282 struct gpd_link *link;
1284 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
1285 return;
1287 if (genpd->suspended_count != genpd->device_count
1288 || atomic_read(&genpd->sd_count) > 0)
1289 return;
1291 /* Check that the children are in their deepest (powered-off) state. */
1292 list_for_each_entry(link, &genpd->parent_links, parent_node) {
1293 struct generic_pm_domain *child = link->child;
1294 if (child->state_idx < child->state_count - 1)
1295 return;
1298 /* Choose the deepest state when suspending */
1299 genpd->state_idx = genpd->state_count - 1;
1300 if (_genpd_power_off(genpd, false)) {
1301 genpd->states[genpd->state_idx].rejected++;
1302 return;
1303 } else {
1304 genpd->states[genpd->state_idx].usage++;
1307 genpd->status = GENPD_STATE_OFF;
1309 list_for_each_entry(link, &genpd->child_links, child_node) {
1310 genpd_sd_counter_dec(link->parent);
1312 if (use_lock)
1313 genpd_lock_nested(link->parent, depth + 1);
1315 genpd_sync_power_off(link->parent, use_lock, depth + 1);
1317 if (use_lock)
1318 genpd_unlock(link->parent);
1323 * genpd_sync_power_on - Synchronously power on a PM domain and its parents.
1324 * @genpd: PM domain to power on.
1325 * @use_lock: use the lock.
1326 * @depth: nesting count for lockdep.
1328 * This function is only called in "noirq" and "syscore" stages of system power
1329 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
1330 * these cases the lock must be held.
1332 static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
1333 unsigned int depth)
1335 struct gpd_link *link;
1337 if (genpd_status_on(genpd))
1338 return;
1340 list_for_each_entry(link, &genpd->child_links, child_node) {
1341 genpd_sd_counter_inc(link->parent);
1343 if (use_lock)
1344 genpd_lock_nested(link->parent, depth + 1);
1346 genpd_sync_power_on(link->parent, use_lock, depth + 1);
1348 if (use_lock)
1349 genpd_unlock(link->parent);
1352 _genpd_power_on(genpd, false);
1353 genpd->status = GENPD_STATE_ON;
1357 * genpd_prepare - Start power transition of a device in a PM domain.
1358 * @dev: Device to start the transition of.
1360 * Start a power transition of a device (during a system-wide power transition)
1361 * under the assumption that its pm_domain field points to the domain member of
1362 * an object of type struct generic_pm_domain representing a PM domain
1363 * consisting of I/O devices.
1365 static int genpd_prepare(struct device *dev)
1367 struct generic_pm_domain *genpd;
1368 int ret;
1370 dev_dbg(dev, "%s()\n", __func__);
1372 genpd = dev_to_genpd(dev);
1373 if (IS_ERR(genpd))
1374 return -EINVAL;
1376 genpd_lock(genpd);
1377 genpd->prepared_count++;
1378 genpd_unlock(genpd);
1380 ret = pm_generic_prepare(dev);
1381 if (ret < 0) {
1382 genpd_lock(genpd);
1384 genpd->prepared_count--;
1386 genpd_unlock(genpd);
1389 /* Never return 1, as genpd don't cope with the direct_complete path. */
1390 return ret >= 0 ? 0 : ret;
1394 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1395 * I/O pm domain.
1396 * @dev: Device to suspend.
1397 * @suspend_noirq: Generic suspend_noirq callback.
1398 * @resume_noirq: Generic resume_noirq callback.
1400 * Stop the device and remove power from the domain if all devices in it have
1401 * been stopped.
1403 static int genpd_finish_suspend(struct device *dev,
1404 int (*suspend_noirq)(struct device *dev),
1405 int (*resume_noirq)(struct device *dev))
1407 struct generic_pm_domain *genpd;
1408 int ret = 0;
1410 genpd = dev_to_genpd(dev);
1411 if (IS_ERR(genpd))
1412 return -EINVAL;
1414 ret = suspend_noirq(dev);
1415 if (ret)
1416 return ret;
1418 if (device_wakeup_path(dev) && genpd_is_active_wakeup(genpd))
1419 return 0;
1421 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1422 !pm_runtime_status_suspended(dev)) {
1423 ret = genpd_stop_dev(genpd, dev);
1424 if (ret) {
1425 resume_noirq(dev);
1426 return ret;
1430 genpd_lock(genpd);
1431 genpd->suspended_count++;
1432 genpd_sync_power_off(genpd, true, 0);
1433 genpd_unlock(genpd);
1435 return 0;
1439 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1440 * @dev: Device to suspend.
1442 * Stop the device and remove power from the domain if all devices in it have
1443 * been stopped.
1445 static int genpd_suspend_noirq(struct device *dev)
1447 dev_dbg(dev, "%s()\n", __func__);
1449 return genpd_finish_suspend(dev,
1450 pm_generic_suspend_noirq,
1451 pm_generic_resume_noirq);
1455 * genpd_finish_resume - Completion of resume of device in an I/O PM domain.
1456 * @dev: Device to resume.
1457 * @resume_noirq: Generic resume_noirq callback.
1459 * Restore power to the device's PM domain, if necessary, and start the device.
1461 static int genpd_finish_resume(struct device *dev,
1462 int (*resume_noirq)(struct device *dev))
1464 struct generic_pm_domain *genpd;
1465 int ret;
1467 dev_dbg(dev, "%s()\n", __func__);
1469 genpd = dev_to_genpd(dev);
1470 if (IS_ERR(genpd))
1471 return -EINVAL;
1473 if (device_wakeup_path(dev) && genpd_is_active_wakeup(genpd))
1474 return resume_noirq(dev);
1476 genpd_lock(genpd);
1477 genpd_sync_power_on(genpd, true, 0);
1478 genpd->suspended_count--;
1479 genpd_unlock(genpd);
1481 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1482 !pm_runtime_status_suspended(dev)) {
1483 ret = genpd_start_dev(genpd, dev);
1484 if (ret)
1485 return ret;
1488 return pm_generic_resume_noirq(dev);
1492 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1493 * @dev: Device to resume.
1495 * Restore power to the device's PM domain, if necessary, and start the device.
1497 static int genpd_resume_noirq(struct device *dev)
1499 dev_dbg(dev, "%s()\n", __func__);
1501 return genpd_finish_resume(dev, pm_generic_resume_noirq);
1505 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1506 * @dev: Device to freeze.
1508 * Carry out a late freeze of a device under the assumption that its
1509 * pm_domain field points to the domain member of an object of type
1510 * struct generic_pm_domain representing a power domain consisting of I/O
1511 * devices.
1513 static int genpd_freeze_noirq(struct device *dev)
1515 dev_dbg(dev, "%s()\n", __func__);
1517 return genpd_finish_suspend(dev,
1518 pm_generic_freeze_noirq,
1519 pm_generic_thaw_noirq);
1523 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1524 * @dev: Device to thaw.
1526 * Start the device, unless power has been removed from the domain already
1527 * before the system transition.
1529 static int genpd_thaw_noirq(struct device *dev)
1531 dev_dbg(dev, "%s()\n", __func__);
1533 return genpd_finish_resume(dev, pm_generic_thaw_noirq);
1537 * genpd_poweroff_noirq - Completion of hibernation of device in an
1538 * I/O PM domain.
1539 * @dev: Device to poweroff.
1541 * Stop the device and remove power from the domain if all devices in it have
1542 * been stopped.
1544 static int genpd_poweroff_noirq(struct device *dev)
1546 dev_dbg(dev, "%s()\n", __func__);
1548 return genpd_finish_suspend(dev,
1549 pm_generic_poweroff_noirq,
1550 pm_generic_restore_noirq);
1554 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1555 * @dev: Device to resume.
1557 * Make sure the domain will be in the same power state as before the
1558 * hibernation the system is resuming from and start the device if necessary.
1560 static int genpd_restore_noirq(struct device *dev)
1562 dev_dbg(dev, "%s()\n", __func__);
1564 return genpd_finish_resume(dev, pm_generic_restore_noirq);
1568 * genpd_complete - Complete power transition of a device in a power domain.
1569 * @dev: Device to complete the transition of.
1571 * Complete a power transition of a device (during a system-wide power
1572 * transition) under the assumption that its pm_domain field points to the
1573 * domain member of an object of type struct generic_pm_domain representing
1574 * a power domain consisting of I/O devices.
1576 static void genpd_complete(struct device *dev)
1578 struct generic_pm_domain *genpd;
1580 dev_dbg(dev, "%s()\n", __func__);
1582 genpd = dev_to_genpd(dev);
1583 if (IS_ERR(genpd))
1584 return;
1586 pm_generic_complete(dev);
1588 genpd_lock(genpd);
1590 genpd->prepared_count--;
1591 if (!genpd->prepared_count)
1592 genpd_queue_power_off_work(genpd);
1594 genpd_unlock(genpd);
1597 static void genpd_switch_state(struct device *dev, bool suspend)
1599 struct generic_pm_domain *genpd;
1600 bool use_lock;
1602 genpd = dev_to_genpd_safe(dev);
1603 if (!genpd)
1604 return;
1606 use_lock = genpd_is_irq_safe(genpd);
1608 if (use_lock)
1609 genpd_lock(genpd);
1611 if (suspend) {
1612 genpd->suspended_count++;
1613 genpd_sync_power_off(genpd, use_lock, 0);
1614 } else {
1615 genpd_sync_power_on(genpd, use_lock, 0);
1616 genpd->suspended_count--;
1619 if (use_lock)
1620 genpd_unlock(genpd);
1624 * dev_pm_genpd_suspend - Synchronously try to suspend the genpd for @dev
1625 * @dev: The device that is attached to the genpd, that can be suspended.
1627 * This routine should typically be called for a device that needs to be
1628 * suspended during the syscore suspend phase. It may also be called during
1629 * suspend-to-idle to suspend a corresponding CPU device that is attached to a
1630 * genpd.
1632 void dev_pm_genpd_suspend(struct device *dev)
1634 genpd_switch_state(dev, true);
1636 EXPORT_SYMBOL_GPL(dev_pm_genpd_suspend);
1639 * dev_pm_genpd_resume - Synchronously try to resume the genpd for @dev
1640 * @dev: The device that is attached to the genpd, which needs to be resumed.
1642 * This routine should typically be called for a device that needs to be resumed
1643 * during the syscore resume phase. It may also be called during suspend-to-idle
1644 * to resume a corresponding CPU device that is attached to a genpd.
1646 void dev_pm_genpd_resume(struct device *dev)
1648 genpd_switch_state(dev, false);
1650 EXPORT_SYMBOL_GPL(dev_pm_genpd_resume);
1652 #else /* !CONFIG_PM_SLEEP */
1654 #define genpd_prepare NULL
1655 #define genpd_suspend_noirq NULL
1656 #define genpd_resume_noirq NULL
1657 #define genpd_freeze_noirq NULL
1658 #define genpd_thaw_noirq NULL
1659 #define genpd_poweroff_noirq NULL
1660 #define genpd_restore_noirq NULL
1661 #define genpd_complete NULL
1663 #endif /* CONFIG_PM_SLEEP */
1665 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1666 bool has_governor)
1668 struct generic_pm_domain_data *gpd_data;
1669 struct gpd_timing_data *td;
1670 int ret;
1672 ret = dev_pm_get_subsys_data(dev);
1673 if (ret)
1674 return ERR_PTR(ret);
1676 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1677 if (!gpd_data) {
1678 ret = -ENOMEM;
1679 goto err_put;
1682 gpd_data->base.dev = dev;
1683 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1685 /* Allocate data used by a governor. */
1686 if (has_governor) {
1687 td = kzalloc(sizeof(*td), GFP_KERNEL);
1688 if (!td) {
1689 ret = -ENOMEM;
1690 goto err_free;
1693 td->constraint_changed = true;
1694 td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
1695 td->next_wakeup = KTIME_MAX;
1696 gpd_data->td = td;
1699 spin_lock_irq(&dev->power.lock);
1701 if (dev->power.subsys_data->domain_data)
1702 ret = -EINVAL;
1703 else
1704 dev->power.subsys_data->domain_data = &gpd_data->base;
1706 spin_unlock_irq(&dev->power.lock);
1708 if (ret)
1709 goto err_free;
1711 return gpd_data;
1713 err_free:
1714 kfree(gpd_data->td);
1715 kfree(gpd_data);
1716 err_put:
1717 dev_pm_put_subsys_data(dev);
1718 return ERR_PTR(ret);
1721 static void genpd_free_dev_data(struct device *dev,
1722 struct generic_pm_domain_data *gpd_data)
1724 spin_lock_irq(&dev->power.lock);
1726 dev->power.subsys_data->domain_data = NULL;
1728 spin_unlock_irq(&dev->power.lock);
1730 dev_pm_opp_clear_config(gpd_data->opp_token);
1731 kfree(gpd_data->td);
1732 kfree(gpd_data);
1733 dev_pm_put_subsys_data(dev);
1736 static void genpd_update_cpumask(struct generic_pm_domain *genpd,
1737 int cpu, bool set, unsigned int depth)
1739 struct gpd_link *link;
1741 if (!genpd_is_cpu_domain(genpd))
1742 return;
1744 list_for_each_entry(link, &genpd->child_links, child_node) {
1745 struct generic_pm_domain *parent = link->parent;
1747 genpd_lock_nested(parent, depth + 1);
1748 genpd_update_cpumask(parent, cpu, set, depth + 1);
1749 genpd_unlock(parent);
1752 if (set)
1753 cpumask_set_cpu(cpu, genpd->cpus);
1754 else
1755 cpumask_clear_cpu(cpu, genpd->cpus);
1758 static void genpd_set_cpumask(struct generic_pm_domain *genpd, int cpu)
1760 if (cpu >= 0)
1761 genpd_update_cpumask(genpd, cpu, true, 0);
1764 static void genpd_clear_cpumask(struct generic_pm_domain *genpd, int cpu)
1766 if (cpu >= 0)
1767 genpd_update_cpumask(genpd, cpu, false, 0);
1770 static int genpd_get_cpu(struct generic_pm_domain *genpd, struct device *dev)
1772 int cpu;
1774 if (!genpd_is_cpu_domain(genpd))
1775 return -1;
1777 for_each_possible_cpu(cpu) {
1778 if (get_cpu_device(cpu) == dev)
1779 return cpu;
1782 return -1;
1785 static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1786 struct device *base_dev)
1788 struct genpd_governor_data *gd = genpd->gd;
1789 struct generic_pm_domain_data *gpd_data;
1790 int ret;
1792 dev_dbg(dev, "%s()\n", __func__);
1794 gpd_data = genpd_alloc_dev_data(dev, gd);
1795 if (IS_ERR(gpd_data))
1796 return PTR_ERR(gpd_data);
1798 gpd_data->cpu = genpd_get_cpu(genpd, base_dev);
1800 gpd_data->hw_mode = genpd->get_hwmode_dev ? genpd->get_hwmode_dev(genpd, dev) : false;
1802 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1803 if (ret)
1804 goto out;
1806 genpd_lock(genpd);
1808 genpd_set_cpumask(genpd, gpd_data->cpu);
1810 genpd->device_count++;
1811 if (gd)
1812 gd->max_off_time_changed = true;
1814 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1816 genpd_unlock(genpd);
1817 dev_pm_domain_set(dev, &genpd->domain);
1818 out:
1819 if (ret)
1820 genpd_free_dev_data(dev, gpd_data);
1821 else
1822 dev_pm_qos_add_notifier(dev, &gpd_data->nb,
1823 DEV_PM_QOS_RESUME_LATENCY);
1825 return ret;
1829 * pm_genpd_add_device - Add a device to an I/O PM domain.
1830 * @genpd: PM domain to add the device to.
1831 * @dev: Device to be added.
1833 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1835 int ret;
1837 if (!genpd || !dev)
1838 return -EINVAL;
1840 mutex_lock(&gpd_list_lock);
1841 ret = genpd_add_device(genpd, dev, dev);
1842 mutex_unlock(&gpd_list_lock);
1844 return ret;
1846 EXPORT_SYMBOL_GPL(pm_genpd_add_device);
1848 static int genpd_remove_device(struct generic_pm_domain *genpd,
1849 struct device *dev)
1851 struct generic_pm_domain_data *gpd_data;
1852 struct pm_domain_data *pdd;
1853 int ret = 0;
1855 dev_dbg(dev, "%s()\n", __func__);
1857 pdd = dev->power.subsys_data->domain_data;
1858 gpd_data = to_gpd_data(pdd);
1859 dev_pm_qos_remove_notifier(dev, &gpd_data->nb,
1860 DEV_PM_QOS_RESUME_LATENCY);
1862 genpd_lock(genpd);
1864 if (genpd->prepared_count > 0) {
1865 ret = -EAGAIN;
1866 goto out;
1869 genpd->device_count--;
1870 if (genpd->gd)
1871 genpd->gd->max_off_time_changed = true;
1873 genpd_clear_cpumask(genpd, gpd_data->cpu);
1875 list_del_init(&pdd->list_node);
1877 genpd_unlock(genpd);
1879 dev_pm_domain_set(dev, NULL);
1881 if (genpd->detach_dev)
1882 genpd->detach_dev(genpd, dev);
1884 genpd_free_dev_data(dev, gpd_data);
1886 return 0;
1888 out:
1889 genpd_unlock(genpd);
1890 dev_pm_qos_add_notifier(dev, &gpd_data->nb, DEV_PM_QOS_RESUME_LATENCY);
1892 return ret;
1896 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1897 * @dev: Device to be removed.
1899 int pm_genpd_remove_device(struct device *dev)
1901 struct generic_pm_domain *genpd = dev_to_genpd_safe(dev);
1903 if (!genpd)
1904 return -EINVAL;
1906 return genpd_remove_device(genpd, dev);
1908 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1911 * dev_pm_genpd_add_notifier - Add a genpd power on/off notifier for @dev
1913 * @dev: Device that should be associated with the notifier
1914 * @nb: The notifier block to register
1916 * Users may call this function to add a genpd power on/off notifier for an
1917 * attached @dev. Only one notifier per device is allowed. The notifier is
1918 * sent when genpd is powering on/off the PM domain.
1920 * It is assumed that the user guarantee that the genpd wouldn't be detached
1921 * while this routine is getting called.
1923 * Returns 0 on success and negative error values on failures.
1925 int dev_pm_genpd_add_notifier(struct device *dev, struct notifier_block *nb)
1927 struct generic_pm_domain *genpd;
1928 struct generic_pm_domain_data *gpd_data;
1929 int ret;
1931 genpd = dev_to_genpd_safe(dev);
1932 if (!genpd)
1933 return -ENODEV;
1935 if (WARN_ON(!dev->power.subsys_data ||
1936 !dev->power.subsys_data->domain_data))
1937 return -EINVAL;
1939 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1940 if (gpd_data->power_nb)
1941 return -EEXIST;
1943 genpd_lock(genpd);
1944 ret = raw_notifier_chain_register(&genpd->power_notifiers, nb);
1945 genpd_unlock(genpd);
1947 if (ret) {
1948 dev_warn(dev, "failed to add notifier for PM domain %s\n",
1949 dev_name(&genpd->dev));
1950 return ret;
1953 gpd_data->power_nb = nb;
1954 return 0;
1956 EXPORT_SYMBOL_GPL(dev_pm_genpd_add_notifier);
1959 * dev_pm_genpd_remove_notifier - Remove a genpd power on/off notifier for @dev
1961 * @dev: Device that is associated with the notifier
1963 * Users may call this function to remove a genpd power on/off notifier for an
1964 * attached @dev.
1966 * It is assumed that the user guarantee that the genpd wouldn't be detached
1967 * while this routine is getting called.
1969 * Returns 0 on success and negative error values on failures.
1971 int dev_pm_genpd_remove_notifier(struct device *dev)
1973 struct generic_pm_domain *genpd;
1974 struct generic_pm_domain_data *gpd_data;
1975 int ret;
1977 genpd = dev_to_genpd_safe(dev);
1978 if (!genpd)
1979 return -ENODEV;
1981 if (WARN_ON(!dev->power.subsys_data ||
1982 !dev->power.subsys_data->domain_data))
1983 return -EINVAL;
1985 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1986 if (!gpd_data->power_nb)
1987 return -ENODEV;
1989 genpd_lock(genpd);
1990 ret = raw_notifier_chain_unregister(&genpd->power_notifiers,
1991 gpd_data->power_nb);
1992 genpd_unlock(genpd);
1994 if (ret) {
1995 dev_warn(dev, "failed to remove notifier for PM domain %s\n",
1996 dev_name(&genpd->dev));
1997 return ret;
2000 gpd_data->power_nb = NULL;
2001 return 0;
2003 EXPORT_SYMBOL_GPL(dev_pm_genpd_remove_notifier);
2005 static int genpd_add_subdomain(struct generic_pm_domain *genpd,
2006 struct generic_pm_domain *subdomain)
2008 struct gpd_link *link, *itr;
2009 int ret = 0;
2011 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
2012 || genpd == subdomain)
2013 return -EINVAL;
2016 * If the domain can be powered on/off in an IRQ safe
2017 * context, ensure that the subdomain can also be
2018 * powered on/off in that context.
2020 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
2021 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
2022 dev_name(&genpd->dev), subdomain->name);
2023 return -EINVAL;
2026 link = kzalloc(sizeof(*link), GFP_KERNEL);
2027 if (!link)
2028 return -ENOMEM;
2030 genpd_lock(subdomain);
2031 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
2033 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
2034 ret = -EINVAL;
2035 goto out;
2038 list_for_each_entry(itr, &genpd->parent_links, parent_node) {
2039 if (itr->child == subdomain && itr->parent == genpd) {
2040 ret = -EINVAL;
2041 goto out;
2045 link->parent = genpd;
2046 list_add_tail(&link->parent_node, &genpd->parent_links);
2047 link->child = subdomain;
2048 list_add_tail(&link->child_node, &subdomain->child_links);
2049 if (genpd_status_on(subdomain))
2050 genpd_sd_counter_inc(genpd);
2052 out:
2053 genpd_unlock(genpd);
2054 genpd_unlock(subdomain);
2055 if (ret)
2056 kfree(link);
2057 return ret;
2061 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2062 * @genpd: Leader PM domain to add the subdomain to.
2063 * @subdomain: Subdomain to be added.
2065 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
2066 struct generic_pm_domain *subdomain)
2068 int ret;
2070 mutex_lock(&gpd_list_lock);
2071 ret = genpd_add_subdomain(genpd, subdomain);
2072 mutex_unlock(&gpd_list_lock);
2074 return ret;
2076 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
2079 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
2080 * @genpd: Leader PM domain to remove the subdomain from.
2081 * @subdomain: Subdomain to be removed.
2083 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
2084 struct generic_pm_domain *subdomain)
2086 struct gpd_link *l, *link;
2087 int ret = -EINVAL;
2089 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
2090 return -EINVAL;
2092 genpd_lock(subdomain);
2093 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
2095 if (!list_empty(&subdomain->parent_links) || subdomain->device_count) {
2096 pr_warn("%s: unable to remove subdomain %s\n",
2097 dev_name(&genpd->dev), subdomain->name);
2098 ret = -EBUSY;
2099 goto out;
2102 list_for_each_entry_safe(link, l, &genpd->parent_links, parent_node) {
2103 if (link->child != subdomain)
2104 continue;
2106 list_del(&link->parent_node);
2107 list_del(&link->child_node);
2108 kfree(link);
2109 if (genpd_status_on(subdomain))
2110 genpd_sd_counter_dec(genpd);
2112 ret = 0;
2113 break;
2116 out:
2117 genpd_unlock(genpd);
2118 genpd_unlock(subdomain);
2120 return ret;
2122 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
2124 static void genpd_free_default_power_state(struct genpd_power_state *states,
2125 unsigned int state_count)
2127 kfree(states);
2130 static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
2132 struct genpd_power_state *state;
2134 state = kzalloc(sizeof(*state), GFP_KERNEL);
2135 if (!state)
2136 return -ENOMEM;
2138 genpd->states = state;
2139 genpd->state_count = 1;
2140 genpd->free_states = genpd_free_default_power_state;
2142 return 0;
2145 static int genpd_alloc_data(struct generic_pm_domain *genpd)
2147 struct genpd_governor_data *gd = NULL;
2148 int ret;
2150 if (genpd_is_cpu_domain(genpd) &&
2151 !zalloc_cpumask_var(&genpd->cpus, GFP_KERNEL))
2152 return -ENOMEM;
2154 if (genpd->gov) {
2155 gd = kzalloc(sizeof(*gd), GFP_KERNEL);
2156 if (!gd) {
2157 ret = -ENOMEM;
2158 goto free;
2161 gd->max_off_time_ns = -1;
2162 gd->max_off_time_changed = true;
2163 gd->next_wakeup = KTIME_MAX;
2164 gd->next_hrtimer = KTIME_MAX;
2167 /* Use only one "off" state if there were no states declared */
2168 if (genpd->state_count == 0) {
2169 ret = genpd_set_default_power_state(genpd);
2170 if (ret)
2171 goto free;
2174 genpd->gd = gd;
2175 return 0;
2177 free:
2178 if (genpd_is_cpu_domain(genpd))
2179 free_cpumask_var(genpd->cpus);
2180 kfree(gd);
2181 return ret;
2184 static void genpd_free_data(struct generic_pm_domain *genpd)
2186 if (genpd_is_cpu_domain(genpd))
2187 free_cpumask_var(genpd->cpus);
2188 if (genpd->free_states)
2189 genpd->free_states(genpd->states, genpd->state_count);
2190 kfree(genpd->gd);
2193 static void genpd_lock_init(struct generic_pm_domain *genpd)
2195 if (genpd_is_cpu_domain(genpd)) {
2196 raw_spin_lock_init(&genpd->raw_slock);
2197 genpd->lock_ops = &genpd_raw_spin_ops;
2198 } else if (genpd_is_irq_safe(genpd)) {
2199 spin_lock_init(&genpd->slock);
2200 genpd->lock_ops = &genpd_spin_ops;
2201 } else {
2202 mutex_init(&genpd->mlock);
2203 genpd->lock_ops = &genpd_mtx_ops;
2208 * pm_genpd_init - Initialize a generic I/O PM domain object.
2209 * @genpd: PM domain object to initialize.
2210 * @gov: PM domain governor to associate with the domain (may be NULL).
2211 * @is_off: Initial value of the domain's power_is_off field.
2213 * Returns 0 on successful initialization, else a negative error code.
2215 int pm_genpd_init(struct generic_pm_domain *genpd,
2216 struct dev_power_governor *gov, bool is_off)
2218 int ret;
2220 if (IS_ERR_OR_NULL(genpd))
2221 return -EINVAL;
2223 INIT_LIST_HEAD(&genpd->parent_links);
2224 INIT_LIST_HEAD(&genpd->child_links);
2225 INIT_LIST_HEAD(&genpd->dev_list);
2226 RAW_INIT_NOTIFIER_HEAD(&genpd->power_notifiers);
2227 genpd_lock_init(genpd);
2228 genpd->gov = gov;
2229 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
2230 atomic_set(&genpd->sd_count, 0);
2231 genpd->status = is_off ? GENPD_STATE_OFF : GENPD_STATE_ON;
2232 genpd->device_count = 0;
2233 genpd->provider = NULL;
2234 genpd->device_id = -ENXIO;
2235 genpd->has_provider = false;
2236 genpd->accounting_time = ktime_get_mono_fast_ns();
2237 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
2238 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
2239 genpd->domain.ops.prepare = genpd_prepare;
2240 genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
2241 genpd->domain.ops.resume_noirq = genpd_resume_noirq;
2242 genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
2243 genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
2244 genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
2245 genpd->domain.ops.restore_noirq = genpd_restore_noirq;
2246 genpd->domain.ops.complete = genpd_complete;
2247 genpd->domain.start = genpd_dev_pm_start;
2248 genpd->domain.set_performance_state = genpd_dev_pm_set_performance_state;
2250 if (genpd->flags & GENPD_FLAG_PM_CLK) {
2251 genpd->dev_ops.stop = pm_clk_suspend;
2252 genpd->dev_ops.start = pm_clk_resume;
2255 /* The always-on governor works better with the corresponding flag. */
2256 if (gov == &pm_domain_always_on_gov)
2257 genpd->flags |= GENPD_FLAG_RPM_ALWAYS_ON;
2259 /* Always-on domains must be powered on at initialization. */
2260 if ((genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd)) &&
2261 !genpd_status_on(genpd)) {
2262 pr_err("always-on PM domain %s is not on\n", genpd->name);
2263 return -EINVAL;
2266 /* Multiple states but no governor doesn't make sense. */
2267 if (!gov && genpd->state_count > 1)
2268 pr_warn("%s: no governor for states\n", genpd->name);
2270 ret = genpd_alloc_data(genpd);
2271 if (ret)
2272 return ret;
2274 device_initialize(&genpd->dev);
2276 if (!genpd_is_dev_name_fw(genpd)) {
2277 dev_set_name(&genpd->dev, "%s", genpd->name);
2278 } else {
2279 ret = ida_alloc(&genpd_ida, GFP_KERNEL);
2280 if (ret < 0) {
2281 put_device(&genpd->dev);
2282 return ret;
2284 genpd->device_id = ret;
2285 dev_set_name(&genpd->dev, "%s_%u", genpd->name, genpd->device_id);
2288 mutex_lock(&gpd_list_lock);
2289 list_add(&genpd->gpd_list_node, &gpd_list);
2290 mutex_unlock(&gpd_list_lock);
2291 genpd_debug_add(genpd);
2293 return 0;
2295 EXPORT_SYMBOL_GPL(pm_genpd_init);
2297 static int genpd_remove(struct generic_pm_domain *genpd)
2299 struct gpd_link *l, *link;
2301 if (IS_ERR_OR_NULL(genpd))
2302 return -EINVAL;
2304 genpd_lock(genpd);
2306 if (genpd->has_provider) {
2307 genpd_unlock(genpd);
2308 pr_err("Provider present, unable to remove %s\n", dev_name(&genpd->dev));
2309 return -EBUSY;
2312 if (!list_empty(&genpd->parent_links) || genpd->device_count) {
2313 genpd_unlock(genpd);
2314 pr_err("%s: unable to remove %s\n", __func__, dev_name(&genpd->dev));
2315 return -EBUSY;
2318 list_for_each_entry_safe(link, l, &genpd->child_links, child_node) {
2319 list_del(&link->parent_node);
2320 list_del(&link->child_node);
2321 kfree(link);
2324 list_del(&genpd->gpd_list_node);
2325 genpd_unlock(genpd);
2326 genpd_debug_remove(genpd);
2327 cancel_work_sync(&genpd->power_off_work);
2328 if (genpd->device_id != -ENXIO)
2329 ida_free(&genpd_ida, genpd->device_id);
2330 genpd_free_data(genpd);
2332 pr_debug("%s: removed %s\n", __func__, dev_name(&genpd->dev));
2334 return 0;
2338 * pm_genpd_remove - Remove a generic I/O PM domain
2339 * @genpd: Pointer to PM domain that is to be removed.
2341 * To remove the PM domain, this function:
2342 * - Removes the PM domain as a subdomain to any parent domains,
2343 * if it was added.
2344 * - Removes the PM domain from the list of registered PM domains.
2346 * The PM domain will only be removed, if the associated provider has
2347 * been removed, it is not a parent to any other PM domain and has no
2348 * devices associated with it.
2350 int pm_genpd_remove(struct generic_pm_domain *genpd)
2352 int ret;
2354 mutex_lock(&gpd_list_lock);
2355 ret = genpd_remove(genpd);
2356 mutex_unlock(&gpd_list_lock);
2358 return ret;
2360 EXPORT_SYMBOL_GPL(pm_genpd_remove);
2362 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
2365 * Device Tree based PM domain providers.
2367 * The code below implements generic device tree based PM domain providers that
2368 * bind device tree nodes with generic PM domains registered in the system.
2370 * Any driver that registers generic PM domains and needs to support binding of
2371 * devices to these domains is supposed to register a PM domain provider, which
2372 * maps a PM domain specifier retrieved from the device tree to a PM domain.
2374 * Two simple mapping functions have been provided for convenience:
2375 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
2376 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
2377 * index.
2381 * struct of_genpd_provider - PM domain provider registration structure
2382 * @link: Entry in global list of PM domain providers
2383 * @node: Pointer to device tree node of PM domain provider
2384 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
2385 * into a PM domain.
2386 * @data: context pointer to be passed into @xlate callback
2388 struct of_genpd_provider {
2389 struct list_head link;
2390 struct device_node *node;
2391 genpd_xlate_t xlate;
2392 void *data;
2395 /* List of registered PM domain providers. */
2396 static LIST_HEAD(of_genpd_providers);
2397 /* Mutex to protect the list above. */
2398 static DEFINE_MUTEX(of_genpd_mutex);
2401 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
2402 * @genpdspec: OF phandle args to map into a PM domain
2403 * @data: xlate function private data - pointer to struct generic_pm_domain
2405 * This is a generic xlate function that can be used to model PM domains that
2406 * have their own device tree nodes. The private data of xlate function needs
2407 * to be a valid pointer to struct generic_pm_domain.
2409 static struct generic_pm_domain *genpd_xlate_simple(
2410 const struct of_phandle_args *genpdspec,
2411 void *data)
2413 return data;
2417 * genpd_xlate_onecell() - Xlate function using a single index.
2418 * @genpdspec: OF phandle args to map into a PM domain
2419 * @data: xlate function private data - pointer to struct genpd_onecell_data
2421 * This is a generic xlate function that can be used to model simple PM domain
2422 * controllers that have one device tree node and provide multiple PM domains.
2423 * A single cell is used as an index into an array of PM domains specified in
2424 * the genpd_onecell_data struct when registering the provider.
2426 static struct generic_pm_domain *genpd_xlate_onecell(
2427 const struct of_phandle_args *genpdspec,
2428 void *data)
2430 struct genpd_onecell_data *genpd_data = data;
2431 unsigned int idx = genpdspec->args[0];
2433 if (genpdspec->args_count != 1)
2434 return ERR_PTR(-EINVAL);
2436 if (idx >= genpd_data->num_domains) {
2437 pr_err("%s: invalid domain index %u\n", __func__, idx);
2438 return ERR_PTR(-EINVAL);
2441 if (!genpd_data->domains[idx])
2442 return ERR_PTR(-ENOENT);
2444 return genpd_data->domains[idx];
2448 * genpd_add_provider() - Register a PM domain provider for a node
2449 * @np: Device node pointer associated with the PM domain provider.
2450 * @xlate: Callback for decoding PM domain from phandle arguments.
2451 * @data: Context pointer for @xlate callback.
2453 static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
2454 void *data)
2456 struct of_genpd_provider *cp;
2458 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2459 if (!cp)
2460 return -ENOMEM;
2462 cp->node = of_node_get(np);
2463 cp->data = data;
2464 cp->xlate = xlate;
2465 fwnode_dev_initialized(&np->fwnode, true);
2467 mutex_lock(&of_genpd_mutex);
2468 list_add(&cp->link, &of_genpd_providers);
2469 mutex_unlock(&of_genpd_mutex);
2470 pr_debug("Added domain provider from %pOF\n", np);
2472 return 0;
2475 static bool genpd_present(const struct generic_pm_domain *genpd)
2477 bool ret = false;
2478 const struct generic_pm_domain *gpd;
2480 mutex_lock(&gpd_list_lock);
2481 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2482 if (gpd == genpd) {
2483 ret = true;
2484 break;
2487 mutex_unlock(&gpd_list_lock);
2489 return ret;
2493 * of_genpd_add_provider_simple() - Register a simple PM domain provider
2494 * @np: Device node pointer associated with the PM domain provider.
2495 * @genpd: Pointer to PM domain associated with the PM domain provider.
2497 int of_genpd_add_provider_simple(struct device_node *np,
2498 struct generic_pm_domain *genpd)
2500 int ret;
2502 if (!np || !genpd)
2503 return -EINVAL;
2505 if (!genpd_present(genpd))
2506 return -EINVAL;
2508 genpd->dev.of_node = np;
2510 /* Parse genpd OPP table */
2511 if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
2512 ret = dev_pm_opp_of_add_table(&genpd->dev);
2513 if (ret)
2514 return dev_err_probe(&genpd->dev, ret, "Failed to add OPP table\n");
2517 * Save table for faster processing while setting performance
2518 * state.
2520 genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
2521 WARN_ON(IS_ERR(genpd->opp_table));
2524 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
2525 if (ret) {
2526 if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
2527 dev_pm_opp_put_opp_table(genpd->opp_table);
2528 dev_pm_opp_of_remove_table(&genpd->dev);
2531 return ret;
2534 genpd->provider = &np->fwnode;
2535 genpd->has_provider = true;
2537 return 0;
2539 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
2542 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
2543 * @np: Device node pointer associated with the PM domain provider.
2544 * @data: Pointer to the data associated with the PM domain provider.
2546 int of_genpd_add_provider_onecell(struct device_node *np,
2547 struct genpd_onecell_data *data)
2549 struct generic_pm_domain *genpd;
2550 unsigned int i;
2551 int ret = -EINVAL;
2553 if (!np || !data)
2554 return -EINVAL;
2556 if (!data->xlate)
2557 data->xlate = genpd_xlate_onecell;
2559 for (i = 0; i < data->num_domains; i++) {
2560 genpd = data->domains[i];
2562 if (!genpd)
2563 continue;
2564 if (!genpd_present(genpd))
2565 goto error;
2567 genpd->dev.of_node = np;
2569 /* Parse genpd OPP table */
2570 if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
2571 ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
2572 if (ret) {
2573 dev_err_probe(&genpd->dev, ret,
2574 "Failed to add OPP table for index %d\n", i);
2575 goto error;
2579 * Save table for faster processing while setting
2580 * performance state.
2582 genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
2583 WARN_ON(IS_ERR(genpd->opp_table));
2586 genpd->provider = &np->fwnode;
2587 genpd->has_provider = true;
2590 ret = genpd_add_provider(np, data->xlate, data);
2591 if (ret < 0)
2592 goto error;
2594 return 0;
2596 error:
2597 while (i--) {
2598 genpd = data->domains[i];
2600 if (!genpd)
2601 continue;
2603 genpd->provider = NULL;
2604 genpd->has_provider = false;
2606 if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
2607 dev_pm_opp_put_opp_table(genpd->opp_table);
2608 dev_pm_opp_of_remove_table(&genpd->dev);
2612 return ret;
2614 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
2617 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2618 * @np: Device node pointer associated with the PM domain provider
2620 void of_genpd_del_provider(struct device_node *np)
2622 struct of_genpd_provider *cp, *tmp;
2623 struct generic_pm_domain *gpd;
2625 mutex_lock(&gpd_list_lock);
2626 mutex_lock(&of_genpd_mutex);
2627 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
2628 if (cp->node == np) {
2630 * For each PM domain associated with the
2631 * provider, set the 'has_provider' to false
2632 * so that the PM domain can be safely removed.
2634 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2635 if (gpd->provider == &np->fwnode) {
2636 gpd->has_provider = false;
2638 if (genpd_is_opp_table_fw(gpd) || !gpd->set_performance_state)
2639 continue;
2641 dev_pm_opp_put_opp_table(gpd->opp_table);
2642 dev_pm_opp_of_remove_table(&gpd->dev);
2646 fwnode_dev_initialized(&cp->node->fwnode, false);
2647 list_del(&cp->link);
2648 of_node_put(cp->node);
2649 kfree(cp);
2650 break;
2653 mutex_unlock(&of_genpd_mutex);
2654 mutex_unlock(&gpd_list_lock);
2656 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2659 * genpd_get_from_provider() - Look-up PM domain
2660 * @genpdspec: OF phandle args to use for look-up
2662 * Looks for a PM domain provider under the node specified by @genpdspec and if
2663 * found, uses xlate function of the provider to map phandle args to a PM
2664 * domain.
2666 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2667 * on failure.
2669 static struct generic_pm_domain *genpd_get_from_provider(
2670 const struct of_phandle_args *genpdspec)
2672 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2673 struct of_genpd_provider *provider;
2675 if (!genpdspec)
2676 return ERR_PTR(-EINVAL);
2678 mutex_lock(&of_genpd_mutex);
2680 /* Check if we have such a provider in our array */
2681 list_for_each_entry(provider, &of_genpd_providers, link) {
2682 if (provider->node == genpdspec->np)
2683 genpd = provider->xlate(genpdspec, provider->data);
2684 if (!IS_ERR(genpd))
2685 break;
2688 mutex_unlock(&of_genpd_mutex);
2690 return genpd;
2694 * of_genpd_add_device() - Add a device to an I/O PM domain
2695 * @genpdspec: OF phandle args to use for look-up PM domain
2696 * @dev: Device to be added.
2698 * Looks-up an I/O PM domain based upon phandle args provided and adds
2699 * the device to the PM domain. Returns a negative error code on failure.
2701 int of_genpd_add_device(const struct of_phandle_args *genpdspec, struct device *dev)
2703 struct generic_pm_domain *genpd;
2704 int ret;
2706 if (!dev)
2707 return -EINVAL;
2709 mutex_lock(&gpd_list_lock);
2711 genpd = genpd_get_from_provider(genpdspec);
2712 if (IS_ERR(genpd)) {
2713 ret = PTR_ERR(genpd);
2714 goto out;
2717 ret = genpd_add_device(genpd, dev, dev);
2719 out:
2720 mutex_unlock(&gpd_list_lock);
2722 return ret;
2724 EXPORT_SYMBOL_GPL(of_genpd_add_device);
2727 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2728 * @parent_spec: OF phandle args to use for parent PM domain look-up
2729 * @subdomain_spec: OF phandle args to use for subdomain look-up
2731 * Looks-up a parent PM domain and subdomain based upon phandle args
2732 * provided and adds the subdomain to the parent PM domain. Returns a
2733 * negative error code on failure.
2735 int of_genpd_add_subdomain(const struct of_phandle_args *parent_spec,
2736 const struct of_phandle_args *subdomain_spec)
2738 struct generic_pm_domain *parent, *subdomain;
2739 int ret;
2741 mutex_lock(&gpd_list_lock);
2743 parent = genpd_get_from_provider(parent_spec);
2744 if (IS_ERR(parent)) {
2745 ret = PTR_ERR(parent);
2746 goto out;
2749 subdomain = genpd_get_from_provider(subdomain_spec);
2750 if (IS_ERR(subdomain)) {
2751 ret = PTR_ERR(subdomain);
2752 goto out;
2755 ret = genpd_add_subdomain(parent, subdomain);
2757 out:
2758 mutex_unlock(&gpd_list_lock);
2760 return ret == -ENOENT ? -EPROBE_DEFER : ret;
2762 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2765 * of_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
2766 * @parent_spec: OF phandle args to use for parent PM domain look-up
2767 * @subdomain_spec: OF phandle args to use for subdomain look-up
2769 * Looks-up a parent PM domain and subdomain based upon phandle args
2770 * provided and removes the subdomain from the parent PM domain. Returns a
2771 * negative error code on failure.
2773 int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
2774 const struct of_phandle_args *subdomain_spec)
2776 struct generic_pm_domain *parent, *subdomain;
2777 int ret;
2779 mutex_lock(&gpd_list_lock);
2781 parent = genpd_get_from_provider(parent_spec);
2782 if (IS_ERR(parent)) {
2783 ret = PTR_ERR(parent);
2784 goto out;
2787 subdomain = genpd_get_from_provider(subdomain_spec);
2788 if (IS_ERR(subdomain)) {
2789 ret = PTR_ERR(subdomain);
2790 goto out;
2793 ret = pm_genpd_remove_subdomain(parent, subdomain);
2795 out:
2796 mutex_unlock(&gpd_list_lock);
2798 return ret;
2800 EXPORT_SYMBOL_GPL(of_genpd_remove_subdomain);
2803 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2804 * @np: Pointer to device node associated with provider
2806 * Find the last PM domain that was added by a particular provider and
2807 * remove this PM domain from the list of PM domains. The provider is
2808 * identified by the 'provider' device structure that is passed. The PM
2809 * domain will only be removed, if the provider associated with domain
2810 * has been removed.
2812 * Returns a valid pointer to struct generic_pm_domain on success or
2813 * ERR_PTR() on failure.
2815 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2817 struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2818 int ret;
2820 if (IS_ERR_OR_NULL(np))
2821 return ERR_PTR(-EINVAL);
2823 mutex_lock(&gpd_list_lock);
2824 list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2825 if (gpd->provider == &np->fwnode) {
2826 ret = genpd_remove(gpd);
2827 genpd = ret ? ERR_PTR(ret) : gpd;
2828 break;
2831 mutex_unlock(&gpd_list_lock);
2833 return genpd;
2835 EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2837 static void genpd_release_dev(struct device *dev)
2839 of_node_put(dev->of_node);
2840 kfree(dev);
2843 static const struct bus_type genpd_bus_type = {
2844 .name = "genpd",
2848 * genpd_dev_pm_detach - Detach a device from its PM domain.
2849 * @dev: Device to detach.
2850 * @power_off: Currently not used
2852 * Try to locate a corresponding generic PM domain, which the device was
2853 * attached to previously. If such is found, the device is detached from it.
2855 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2857 struct generic_pm_domain *pd;
2858 unsigned int i;
2859 int ret = 0;
2861 pd = dev_to_genpd(dev);
2862 if (IS_ERR(pd))
2863 return;
2865 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2867 /* Drop the default performance state */
2868 if (dev_gpd_data(dev)->default_pstate) {
2869 dev_pm_genpd_set_performance_state(dev, 0);
2870 dev_gpd_data(dev)->default_pstate = 0;
2873 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2874 ret = genpd_remove_device(pd, dev);
2875 if (ret != -EAGAIN)
2876 break;
2878 mdelay(i);
2879 cond_resched();
2882 if (ret < 0) {
2883 dev_err(dev, "failed to remove from PM domain %s: %d",
2884 pd->name, ret);
2885 return;
2888 /* Check if PM domain can be powered off after removing this device. */
2889 genpd_queue_power_off_work(pd);
2891 /* Unregister the device if it was created by genpd. */
2892 if (dev->bus == &genpd_bus_type)
2893 device_unregister(dev);
2896 static void genpd_dev_pm_sync(struct device *dev)
2898 struct generic_pm_domain *pd;
2900 pd = dev_to_genpd(dev);
2901 if (IS_ERR(pd))
2902 return;
2904 genpd_queue_power_off_work(pd);
2907 static int genpd_set_required_opp_dev(struct device *dev,
2908 struct device *base_dev)
2910 struct dev_pm_opp_config config = {
2911 .required_dev = dev,
2913 int ret;
2915 /* Limit support to non-providers for now. */
2916 if (of_property_present(base_dev->of_node, "#power-domain-cells"))
2917 return 0;
2919 if (!dev_pm_opp_of_has_required_opp(base_dev))
2920 return 0;
2922 ret = dev_pm_opp_set_config(base_dev, &config);
2923 if (ret < 0)
2924 return ret;
2926 dev_gpd_data(dev)->opp_token = ret;
2927 return 0;
2930 static int genpd_set_required_opp(struct device *dev, unsigned int index)
2932 int ret, pstate;
2934 /* Set the default performance state */
2935 pstate = of_get_required_opp_performance_state(dev->of_node, index);
2936 if (pstate < 0 && pstate != -ENODEV && pstate != -EOPNOTSUPP) {
2937 ret = pstate;
2938 goto err;
2939 } else if (pstate > 0) {
2940 ret = dev_pm_genpd_set_performance_state(dev, pstate);
2941 if (ret)
2942 goto err;
2943 dev_gpd_data(dev)->default_pstate = pstate;
2946 return 0;
2947 err:
2948 dev_err(dev, "failed to set required performance state for power-domain %s: %d\n",
2949 dev_to_genpd(dev)->name, ret);
2950 return ret;
2953 static int __genpd_dev_pm_attach(struct device *dev, struct device *base_dev,
2954 unsigned int index, unsigned int num_domains,
2955 bool power_on)
2957 struct of_phandle_args pd_args;
2958 struct generic_pm_domain *pd;
2959 int ret;
2961 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2962 "#power-domain-cells", index, &pd_args);
2963 if (ret < 0)
2964 return ret;
2966 mutex_lock(&gpd_list_lock);
2967 pd = genpd_get_from_provider(&pd_args);
2968 of_node_put(pd_args.np);
2969 if (IS_ERR(pd)) {
2970 mutex_unlock(&gpd_list_lock);
2971 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2972 __func__, PTR_ERR(pd));
2973 return driver_deferred_probe_check_state(base_dev);
2976 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2978 ret = genpd_add_device(pd, dev, base_dev);
2979 mutex_unlock(&gpd_list_lock);
2981 if (ret < 0)
2982 return dev_err_probe(dev, ret, "failed to add to PM domain %s\n", pd->name);
2984 dev->pm_domain->detach = genpd_dev_pm_detach;
2985 dev->pm_domain->sync = genpd_dev_pm_sync;
2988 * For a single PM domain the index of the required OPP must be zero, so
2989 * let's try to assign a required dev in that case. In the multiple PM
2990 * domains case, we need platform code to specify the index.
2992 if (num_domains == 1) {
2993 ret = genpd_set_required_opp_dev(dev, base_dev);
2994 if (ret)
2995 goto err;
2998 ret = genpd_set_required_opp(dev, index);
2999 if (ret)
3000 goto err;
3002 if (power_on) {
3003 genpd_lock(pd);
3004 ret = genpd_power_on(pd, 0);
3005 genpd_unlock(pd);
3008 if (ret) {
3009 /* Drop the default performance state */
3010 if (dev_gpd_data(dev)->default_pstate) {
3011 dev_pm_genpd_set_performance_state(dev, 0);
3012 dev_gpd_data(dev)->default_pstate = 0;
3015 genpd_remove_device(pd, dev);
3016 return -EPROBE_DEFER;
3019 return 1;
3021 err:
3022 genpd_remove_device(pd, dev);
3023 return ret;
3027 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
3028 * @dev: Device to attach.
3030 * Parse device's OF node to find a PM domain specifier. If such is found,
3031 * attaches the device to retrieved pm_domain ops.
3033 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
3034 * PM domain or when multiple power-domains exists for it, else a negative error
3035 * code. Note that if a power-domain exists for the device, but it cannot be
3036 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
3037 * not probed and to re-try again later.
3039 int genpd_dev_pm_attach(struct device *dev)
3041 if (!dev->of_node)
3042 return 0;
3045 * Devices with multiple PM domains must be attached separately, as we
3046 * can only attach one PM domain per device.
3048 if (of_count_phandle_with_args(dev->of_node, "power-domains",
3049 "#power-domain-cells") != 1)
3050 return 0;
3052 return __genpd_dev_pm_attach(dev, dev, 0, 1, true);
3054 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
3057 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
3058 * @dev: The device used to lookup the PM domain.
3059 * @index: The index of the PM domain.
3061 * Parse device's OF node to find a PM domain specifier at the provided @index.
3062 * If such is found, creates a virtual device and attaches it to the retrieved
3063 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
3064 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
3066 * Returns the created virtual device if successfully attached PM domain, NULL
3067 * when the device don't need a PM domain, else an ERR_PTR() in case of
3068 * failures. If a power-domain exists for the device, but cannot be found or
3069 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
3070 * is not probed and to re-try again later.
3072 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
3073 unsigned int index)
3075 struct device *virt_dev;
3076 int num_domains;
3077 int ret;
3079 if (!dev->of_node)
3080 return NULL;
3082 /* Verify that the index is within a valid range. */
3083 num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
3084 "#power-domain-cells");
3085 if (index >= num_domains)
3086 return NULL;
3088 /* Allocate and register device on the genpd bus. */
3089 virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL);
3090 if (!virt_dev)
3091 return ERR_PTR(-ENOMEM);
3093 dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev));
3094 virt_dev->bus = &genpd_bus_type;
3095 virt_dev->release = genpd_release_dev;
3096 virt_dev->of_node = of_node_get(dev->of_node);
3098 ret = device_register(virt_dev);
3099 if (ret) {
3100 put_device(virt_dev);
3101 return ERR_PTR(ret);
3104 /* Try to attach the device to the PM domain at the specified index. */
3105 ret = __genpd_dev_pm_attach(virt_dev, dev, index, num_domains, false);
3106 if (ret < 1) {
3107 device_unregister(virt_dev);
3108 return ret ? ERR_PTR(ret) : NULL;
3111 pm_runtime_enable(virt_dev);
3112 genpd_queue_power_off_work(dev_to_genpd(virt_dev));
3114 return virt_dev;
3116 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
3119 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
3120 * @dev: The device used to lookup the PM domain.
3121 * @name: The name of the PM domain.
3123 * Parse device's OF node to find a PM domain specifier using the
3124 * power-domain-names DT property. For further description see
3125 * genpd_dev_pm_attach_by_id().
3127 struct device *genpd_dev_pm_attach_by_name(struct device *dev, const char *name)
3129 int index;
3131 if (!dev->of_node)
3132 return NULL;
3134 index = of_property_match_string(dev->of_node, "power-domain-names",
3135 name);
3136 if (index < 0)
3137 return NULL;
3139 return genpd_dev_pm_attach_by_id(dev, index);
3142 static const struct of_device_id idle_state_match[] = {
3143 { .compatible = "domain-idle-state", },
3147 static int genpd_parse_state(struct genpd_power_state *genpd_state,
3148 struct device_node *state_node)
3150 int err;
3151 u32 residency;
3152 u32 entry_latency, exit_latency;
3154 err = of_property_read_u32(state_node, "entry-latency-us",
3155 &entry_latency);
3156 if (err) {
3157 pr_debug(" * %pOF missing entry-latency-us property\n",
3158 state_node);
3159 return -EINVAL;
3162 err = of_property_read_u32(state_node, "exit-latency-us",
3163 &exit_latency);
3164 if (err) {
3165 pr_debug(" * %pOF missing exit-latency-us property\n",
3166 state_node);
3167 return -EINVAL;
3170 err = of_property_read_u32(state_node, "min-residency-us", &residency);
3171 if (!err)
3172 genpd_state->residency_ns = 1000LL * residency;
3174 genpd_state->power_on_latency_ns = 1000LL * exit_latency;
3175 genpd_state->power_off_latency_ns = 1000LL * entry_latency;
3176 genpd_state->fwnode = &state_node->fwnode;
3178 return 0;
3181 static int genpd_iterate_idle_states(struct device_node *dn,
3182 struct genpd_power_state *states)
3184 int ret;
3185 struct of_phandle_iterator it;
3186 struct device_node *np;
3187 int i = 0;
3189 ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
3190 if (ret <= 0)
3191 return ret == -ENOENT ? 0 : ret;
3193 /* Loop over the phandles until all the requested entry is found */
3194 of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
3195 np = it.node;
3196 if (!of_match_node(idle_state_match, np))
3197 continue;
3199 if (!of_device_is_available(np))
3200 continue;
3202 if (states) {
3203 ret = genpd_parse_state(&states[i], np);
3204 if (ret) {
3205 pr_err("Parsing idle state node %pOF failed with err %d\n",
3206 np, ret);
3207 of_node_put(np);
3208 return ret;
3211 i++;
3214 return i;
3218 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
3220 * @dn: The genpd device node
3221 * @states: The pointer to which the state array will be saved.
3222 * @n: The count of elements in the array returned from this function.
3224 * Returns the device states parsed from the OF node. The memory for the states
3225 * is allocated by this function and is the responsibility of the caller to
3226 * free the memory after use. If any or zero compatible domain idle states is
3227 * found it returns 0 and in case of errors, a negative error code is returned.
3229 int of_genpd_parse_idle_states(struct device_node *dn,
3230 struct genpd_power_state **states, int *n)
3232 struct genpd_power_state *st;
3233 int ret;
3235 ret = genpd_iterate_idle_states(dn, NULL);
3236 if (ret < 0)
3237 return ret;
3239 if (!ret) {
3240 *states = NULL;
3241 *n = 0;
3242 return 0;
3245 st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
3246 if (!st)
3247 return -ENOMEM;
3249 ret = genpd_iterate_idle_states(dn, st);
3250 if (ret <= 0) {
3251 kfree(st);
3252 return ret < 0 ? ret : -EINVAL;
3255 *states = st;
3256 *n = ret;
3258 return 0;
3260 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
3262 static int __init genpd_bus_init(void)
3264 return bus_register(&genpd_bus_type);
3266 core_initcall(genpd_bus_init);
3268 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
3271 /*** debugfs support ***/
3273 #ifdef CONFIG_DEBUG_FS
3275 * TODO: This function is a slightly modified version of rtpm_status_show
3276 * from sysfs.c, so generalize it.
3278 static void rtpm_status_str(struct seq_file *s, struct device *dev)
3280 static const char * const status_lookup[] = {
3281 [RPM_ACTIVE] = "active",
3282 [RPM_RESUMING] = "resuming",
3283 [RPM_SUSPENDED] = "suspended",
3284 [RPM_SUSPENDING] = "suspending"
3286 const char *p = "";
3288 if (dev->power.runtime_error)
3289 p = "error";
3290 else if (dev->power.disable_depth)
3291 p = "unsupported";
3292 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
3293 p = status_lookup[dev->power.runtime_status];
3294 else
3295 WARN_ON(1);
3297 seq_printf(s, "%-26s ", p);
3300 static void perf_status_str(struct seq_file *s, struct device *dev)
3302 struct generic_pm_domain_data *gpd_data;
3304 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
3306 seq_printf(s, "%-10u ", gpd_data->performance_state);
3309 static void mode_status_str(struct seq_file *s, struct device *dev)
3311 struct generic_pm_domain_data *gpd_data;
3313 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
3315 seq_printf(s, "%2s", gpd_data->hw_mode ? "HW" : "SW");
3318 static int genpd_summary_one(struct seq_file *s,
3319 struct generic_pm_domain *genpd)
3321 static const char * const status_lookup[] = {
3322 [GENPD_STATE_ON] = "on",
3323 [GENPD_STATE_OFF] = "off"
3325 struct pm_domain_data *pm_data;
3326 struct gpd_link *link;
3327 char state[16];
3328 int ret;
3330 ret = genpd_lock_interruptible(genpd);
3331 if (ret)
3332 return -ERESTARTSYS;
3334 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
3335 goto exit;
3336 if (!genpd_status_on(genpd))
3337 snprintf(state, sizeof(state), "%s-%u",
3338 status_lookup[genpd->status], genpd->state_idx);
3339 else
3340 snprintf(state, sizeof(state), "%s",
3341 status_lookup[genpd->status]);
3342 seq_printf(s, "%-30s %-30s %u", dev_name(&genpd->dev), state, genpd->performance_state);
3345 * Modifications on the list require holding locks on both
3346 * parent and child, so we are safe.
3347 * Also the device name is immutable.
3349 list_for_each_entry(link, &genpd->parent_links, parent_node) {
3350 if (list_is_first(&link->parent_node, &genpd->parent_links))
3351 seq_printf(s, "\n%48s", " ");
3352 seq_printf(s, "%s", link->child->name);
3353 if (!list_is_last(&link->parent_node, &genpd->parent_links))
3354 seq_puts(s, ", ");
3357 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
3358 seq_printf(s, "\n %-30s ", dev_name(pm_data->dev));
3359 rtpm_status_str(s, pm_data->dev);
3360 perf_status_str(s, pm_data->dev);
3361 mode_status_str(s, pm_data->dev);
3364 seq_puts(s, "\n");
3365 exit:
3366 genpd_unlock(genpd);
3368 return 0;
3371 static int summary_show(struct seq_file *s, void *data)
3373 struct generic_pm_domain *genpd;
3374 int ret = 0;
3376 seq_puts(s, "domain status children performance\n");
3377 seq_puts(s, " /device runtime status managed by\n");
3378 seq_puts(s, "------------------------------------------------------------------------------\n");
3380 ret = mutex_lock_interruptible(&gpd_list_lock);
3381 if (ret)
3382 return -ERESTARTSYS;
3384 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
3385 ret = genpd_summary_one(s, genpd);
3386 if (ret)
3387 break;
3389 mutex_unlock(&gpd_list_lock);
3391 return ret;
3394 static int status_show(struct seq_file *s, void *data)
3396 static const char * const status_lookup[] = {
3397 [GENPD_STATE_ON] = "on",
3398 [GENPD_STATE_OFF] = "off"
3401 struct generic_pm_domain *genpd = s->private;
3402 int ret = 0;
3404 ret = genpd_lock_interruptible(genpd);
3405 if (ret)
3406 return -ERESTARTSYS;
3408 if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
3409 goto exit;
3411 if (genpd->status == GENPD_STATE_OFF)
3412 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
3413 genpd->state_idx);
3414 else
3415 seq_printf(s, "%s\n", status_lookup[genpd->status]);
3416 exit:
3417 genpd_unlock(genpd);
3418 return ret;
3421 static int sub_domains_show(struct seq_file *s, void *data)
3423 struct generic_pm_domain *genpd = s->private;
3424 struct gpd_link *link;
3425 int ret = 0;
3427 ret = genpd_lock_interruptible(genpd);
3428 if (ret)
3429 return -ERESTARTSYS;
3431 list_for_each_entry(link, &genpd->parent_links, parent_node)
3432 seq_printf(s, "%s\n", link->child->name);
3434 genpd_unlock(genpd);
3435 return ret;
3438 static int idle_states_show(struct seq_file *s, void *data)
3440 struct generic_pm_domain *genpd = s->private;
3441 u64 now, delta, idle_time = 0;
3442 unsigned int i;
3443 int ret = 0;
3445 ret = genpd_lock_interruptible(genpd);
3446 if (ret)
3447 return -ERESTARTSYS;
3449 seq_puts(s, "State Time Spent(ms) Usage Rejected\n");
3451 for (i = 0; i < genpd->state_count; i++) {
3452 idle_time += genpd->states[i].idle_time;
3454 if (genpd->status == GENPD_STATE_OFF && genpd->state_idx == i) {
3455 now = ktime_get_mono_fast_ns();
3456 if (now > genpd->accounting_time) {
3457 delta = now - genpd->accounting_time;
3458 idle_time += delta;
3462 do_div(idle_time, NSEC_PER_MSEC);
3463 seq_printf(s, "S%-13i %-14llu %-14llu %llu\n", i, idle_time,
3464 genpd->states[i].usage, genpd->states[i].rejected);
3467 genpd_unlock(genpd);
3468 return ret;
3471 static int active_time_show(struct seq_file *s, void *data)
3473 struct generic_pm_domain *genpd = s->private;
3474 u64 now, on_time, delta = 0;
3475 int ret = 0;
3477 ret = genpd_lock_interruptible(genpd);
3478 if (ret)
3479 return -ERESTARTSYS;
3481 if (genpd->status == GENPD_STATE_ON) {
3482 now = ktime_get_mono_fast_ns();
3483 if (now > genpd->accounting_time)
3484 delta = now - genpd->accounting_time;
3487 on_time = genpd->on_time + delta;
3488 do_div(on_time, NSEC_PER_MSEC);
3489 seq_printf(s, "%llu ms\n", on_time);
3491 genpd_unlock(genpd);
3492 return ret;
3495 static int total_idle_time_show(struct seq_file *s, void *data)
3497 struct generic_pm_domain *genpd = s->private;
3498 u64 now, delta, total = 0;
3499 unsigned int i;
3500 int ret = 0;
3502 ret = genpd_lock_interruptible(genpd);
3503 if (ret)
3504 return -ERESTARTSYS;
3506 for (i = 0; i < genpd->state_count; i++) {
3507 total += genpd->states[i].idle_time;
3509 if (genpd->status == GENPD_STATE_OFF && genpd->state_idx == i) {
3510 now = ktime_get_mono_fast_ns();
3511 if (now > genpd->accounting_time) {
3512 delta = now - genpd->accounting_time;
3513 total += delta;
3518 do_div(total, NSEC_PER_MSEC);
3519 seq_printf(s, "%llu ms\n", total);
3521 genpd_unlock(genpd);
3522 return ret;
3526 static int devices_show(struct seq_file *s, void *data)
3528 struct generic_pm_domain *genpd = s->private;
3529 struct pm_domain_data *pm_data;
3530 int ret = 0;
3532 ret = genpd_lock_interruptible(genpd);
3533 if (ret)
3534 return -ERESTARTSYS;
3536 list_for_each_entry(pm_data, &genpd->dev_list, list_node)
3537 seq_printf(s, "%s\n", dev_name(pm_data->dev));
3539 genpd_unlock(genpd);
3540 return ret;
3543 static int perf_state_show(struct seq_file *s, void *data)
3545 struct generic_pm_domain *genpd = s->private;
3547 if (genpd_lock_interruptible(genpd))
3548 return -ERESTARTSYS;
3550 seq_printf(s, "%u\n", genpd->performance_state);
3552 genpd_unlock(genpd);
3553 return 0;
3556 DEFINE_SHOW_ATTRIBUTE(summary);
3557 DEFINE_SHOW_ATTRIBUTE(status);
3558 DEFINE_SHOW_ATTRIBUTE(sub_domains);
3559 DEFINE_SHOW_ATTRIBUTE(idle_states);
3560 DEFINE_SHOW_ATTRIBUTE(active_time);
3561 DEFINE_SHOW_ATTRIBUTE(total_idle_time);
3562 DEFINE_SHOW_ATTRIBUTE(devices);
3563 DEFINE_SHOW_ATTRIBUTE(perf_state);
3565 static void genpd_debug_add(struct generic_pm_domain *genpd)
3567 struct dentry *d;
3569 if (!genpd_debugfs_dir)
3570 return;
3572 d = debugfs_create_dir(dev_name(&genpd->dev), genpd_debugfs_dir);
3574 debugfs_create_file("current_state", 0444,
3575 d, genpd, &status_fops);
3576 debugfs_create_file("sub_domains", 0444,
3577 d, genpd, &sub_domains_fops);
3578 debugfs_create_file("idle_states", 0444,
3579 d, genpd, &idle_states_fops);
3580 debugfs_create_file("active_time", 0444,
3581 d, genpd, &active_time_fops);
3582 debugfs_create_file("total_idle_time", 0444,
3583 d, genpd, &total_idle_time_fops);
3584 debugfs_create_file("devices", 0444,
3585 d, genpd, &devices_fops);
3586 if (genpd->set_performance_state)
3587 debugfs_create_file("perf_state", 0444,
3588 d, genpd, &perf_state_fops);
3591 static int __init genpd_debug_init(void)
3593 struct generic_pm_domain *genpd;
3595 genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
3597 debugfs_create_file("pm_genpd_summary", S_IRUGO, genpd_debugfs_dir,
3598 NULL, &summary_fops);
3600 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
3601 genpd_debug_add(genpd);
3603 return 0;
3605 late_initcall(genpd_debug_init);
3607 static void __exit genpd_debug_exit(void)
3609 debugfs_remove_recursive(genpd_debugfs_dir);
3611 __exitcall(genpd_debug_exit);
3612 #endif /* CONFIG_DEBUG_FS */