f81232: switch to ->get_serial()
[linux/fpc-iii.git] / drivers / base / power / domain.c
blob4b5714199490802d626df3c34952925be0ee6e77
1 /*
2 * drivers/base/power/domain.c - Common code related to device power domains.
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 * This file is released under the GPLv2.
7 */
9 #include <linux/delay.h>
10 #include <linux/kernel.h>
11 #include <linux/io.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_opp.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/pm_domain.h>
16 #include <linux/pm_qos.h>
17 #include <linux/pm_clock.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/sched.h>
21 #include <linux/suspend.h>
22 #include <linux/export.h>
24 #include "power.h"
26 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
28 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
29 ({ \
30 type (*__routine)(struct device *__d); \
31 type __ret = (type)0; \
33 __routine = genpd->dev_ops.callback; \
34 if (__routine) { \
35 __ret = __routine(dev); \
36 } \
37 __ret; \
40 static LIST_HEAD(gpd_list);
41 static DEFINE_MUTEX(gpd_list_lock);
43 struct genpd_lock_ops {
44 void (*lock)(struct generic_pm_domain *genpd);
45 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
46 int (*lock_interruptible)(struct generic_pm_domain *genpd);
47 void (*unlock)(struct generic_pm_domain *genpd);
50 static void genpd_lock_mtx(struct generic_pm_domain *genpd)
52 mutex_lock(&genpd->mlock);
55 static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
56 int depth)
58 mutex_lock_nested(&genpd->mlock, depth);
61 static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
63 return mutex_lock_interruptible(&genpd->mlock);
66 static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
68 return mutex_unlock(&genpd->mlock);
71 static const struct genpd_lock_ops genpd_mtx_ops = {
72 .lock = genpd_lock_mtx,
73 .lock_nested = genpd_lock_nested_mtx,
74 .lock_interruptible = genpd_lock_interruptible_mtx,
75 .unlock = genpd_unlock_mtx,
78 static void genpd_lock_spin(struct generic_pm_domain *genpd)
79 __acquires(&genpd->slock)
81 unsigned long flags;
83 spin_lock_irqsave(&genpd->slock, flags);
84 genpd->lock_flags = flags;
87 static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
88 int depth)
89 __acquires(&genpd->slock)
91 unsigned long flags;
93 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
94 genpd->lock_flags = flags;
97 static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
98 __acquires(&genpd->slock)
100 unsigned long flags;
102 spin_lock_irqsave(&genpd->slock, flags);
103 genpd->lock_flags = flags;
104 return 0;
107 static void genpd_unlock_spin(struct generic_pm_domain *genpd)
108 __releases(&genpd->slock)
110 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
113 static const struct genpd_lock_ops genpd_spin_ops = {
114 .lock = genpd_lock_spin,
115 .lock_nested = genpd_lock_nested_spin,
116 .lock_interruptible = genpd_lock_interruptible_spin,
117 .unlock = genpd_unlock_spin,
120 #define genpd_lock(p) p->lock_ops->lock(p)
121 #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
122 #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
123 #define genpd_unlock(p) p->lock_ops->unlock(p)
125 #define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
126 #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
127 #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
128 #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
130 static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
131 const struct generic_pm_domain *genpd)
133 bool ret;
135 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
138 * Warn once if an IRQ safe device is attached to a no sleep domain, as
139 * to indicate a suboptimal configuration for PM. For an always on
140 * domain this isn't case, thus don't warn.
142 if (ret && !genpd_is_always_on(genpd))
143 dev_warn_once(dev, "PM domain %s will not be powered off\n",
144 genpd->name);
146 return ret;
150 * Get the generic PM domain for a particular struct device.
151 * This validates the struct device pointer, the PM domain pointer,
152 * and checks that the PM domain pointer is a real generic PM domain.
153 * Any failure results in NULL being returned.
155 static struct generic_pm_domain *genpd_lookup_dev(struct device *dev)
157 struct generic_pm_domain *genpd = NULL, *gpd;
159 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
160 return NULL;
162 mutex_lock(&gpd_list_lock);
163 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
164 if (&gpd->domain == dev->pm_domain) {
165 genpd = gpd;
166 break;
169 mutex_unlock(&gpd_list_lock);
171 return genpd;
175 * This should only be used where we are certain that the pm_domain
176 * attached to the device is a genpd domain.
178 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
180 if (IS_ERR_OR_NULL(dev->pm_domain))
181 return ERR_PTR(-EINVAL);
183 return pd_to_genpd(dev->pm_domain);
186 static int genpd_stop_dev(const struct generic_pm_domain *genpd,
187 struct device *dev)
189 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
192 static int genpd_start_dev(const struct generic_pm_domain *genpd,
193 struct device *dev)
195 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
198 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
200 bool ret = false;
202 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
203 ret = !!atomic_dec_and_test(&genpd->sd_count);
205 return ret;
208 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
210 atomic_inc(&genpd->sd_count);
211 smp_mb__after_atomic();
214 #ifdef CONFIG_DEBUG_FS
215 static void genpd_update_accounting(struct generic_pm_domain *genpd)
217 ktime_t delta, now;
219 now = ktime_get();
220 delta = ktime_sub(now, genpd->accounting_time);
223 * If genpd->status is active, it means we are just
224 * out of off and so update the idle time and vice
225 * versa.
227 if (genpd->status == GPD_STATE_ACTIVE) {
228 int state_idx = genpd->state_idx;
230 genpd->states[state_idx].idle_time =
231 ktime_add(genpd->states[state_idx].idle_time, delta);
232 } else {
233 genpd->on_time = ktime_add(genpd->on_time, delta);
236 genpd->accounting_time = now;
238 #else
239 static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
240 #endif
243 * dev_pm_genpd_set_performance_state- Set performance state of device's power
244 * domain.
246 * @dev: Device for which the performance-state needs to be set.
247 * @state: Target performance state of the device. This can be set as 0 when the
248 * device doesn't have any performance state constraints left (And so
249 * the device wouldn't participate anymore to find the target
250 * performance state of the genpd).
252 * It is assumed that the users guarantee that the genpd wouldn't be detached
253 * while this routine is getting called.
255 * Returns 0 on success and negative error values on failures.
257 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
259 struct generic_pm_domain *genpd;
260 struct generic_pm_domain_data *gpd_data, *pd_data;
261 struct pm_domain_data *pdd;
262 unsigned int prev;
263 int ret = 0;
265 genpd = dev_to_genpd(dev);
266 if (IS_ERR(genpd))
267 return -ENODEV;
269 if (unlikely(!genpd->set_performance_state))
270 return -EINVAL;
272 if (unlikely(!dev->power.subsys_data ||
273 !dev->power.subsys_data->domain_data)) {
274 WARN_ON(1);
275 return -EINVAL;
278 genpd_lock(genpd);
280 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
281 prev = gpd_data->performance_state;
282 gpd_data->performance_state = state;
284 /* New requested state is same as Max requested state */
285 if (state == genpd->performance_state)
286 goto unlock;
288 /* New requested state is higher than Max requested state */
289 if (state > genpd->performance_state)
290 goto update_state;
292 /* Traverse all devices within the domain */
293 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
294 pd_data = to_gpd_data(pdd);
296 if (pd_data->performance_state > state)
297 state = pd_data->performance_state;
300 if (state == genpd->performance_state)
301 goto unlock;
304 * We aren't propagating performance state changes of a subdomain to its
305 * masters as we don't have hardware that needs it. Over that, the
306 * performance states of subdomain and its masters may not have
307 * one-to-one mapping and would require additional information. We can
308 * get back to this once we have hardware that needs it. For that
309 * reason, we don't have to consider performance state of the subdomains
310 * of genpd here.
313 update_state:
314 if (genpd_status_on(genpd)) {
315 ret = genpd->set_performance_state(genpd, state);
316 if (ret) {
317 gpd_data->performance_state = prev;
318 goto unlock;
322 genpd->performance_state = state;
324 unlock:
325 genpd_unlock(genpd);
327 return ret;
329 EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
331 static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
333 unsigned int state_idx = genpd->state_idx;
334 ktime_t time_start;
335 s64 elapsed_ns;
336 int ret;
338 if (!genpd->power_on)
339 return 0;
341 if (!timed)
342 return genpd->power_on(genpd);
344 time_start = ktime_get();
345 ret = genpd->power_on(genpd);
346 if (ret)
347 return ret;
349 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
351 if (unlikely(genpd->set_performance_state)) {
352 ret = genpd->set_performance_state(genpd, genpd->performance_state);
353 if (ret) {
354 pr_warn("%s: Failed to set performance state %d (%d)\n",
355 genpd->name, genpd->performance_state, ret);
359 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
360 return ret;
362 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
363 genpd->max_off_time_changed = true;
364 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
365 genpd->name, "on", elapsed_ns);
367 return ret;
370 static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
372 unsigned int state_idx = genpd->state_idx;
373 ktime_t time_start;
374 s64 elapsed_ns;
375 int ret;
377 if (!genpd->power_off)
378 return 0;
380 if (!timed)
381 return genpd->power_off(genpd);
383 time_start = ktime_get();
384 ret = genpd->power_off(genpd);
385 if (ret == -EBUSY)
386 return ret;
388 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
389 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
390 return ret;
392 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
393 genpd->max_off_time_changed = true;
394 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
395 genpd->name, "off", elapsed_ns);
397 return ret;
401 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
402 * @genpd: PM domain to power off.
404 * Queue up the execution of genpd_power_off() unless it's already been done
405 * before.
407 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
409 queue_work(pm_wq, &genpd->power_off_work);
413 * genpd_power_off - Remove power from a given PM domain.
414 * @genpd: PM domain to power down.
415 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
416 * RPM status of the releated device is in an intermediate state, not yet turned
417 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
418 * be RPM_SUSPENDED, while it tries to power off the PM domain.
420 * If all of the @genpd's devices have been suspended and all of its subdomains
421 * have been powered down, remove power from @genpd.
423 static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
424 unsigned int depth)
426 struct pm_domain_data *pdd;
427 struct gpd_link *link;
428 unsigned int not_suspended = 0;
431 * Do not try to power off the domain in the following situations:
432 * (1) The domain is already in the "power off" state.
433 * (2) System suspend is in progress.
435 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
436 return 0;
439 * Abort power off for the PM domain in the following situations:
440 * (1) The domain is configured as always on.
441 * (2) When the domain has a subdomain being powered on.
443 if (genpd_is_always_on(genpd) || atomic_read(&genpd->sd_count) > 0)
444 return -EBUSY;
446 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
447 enum pm_qos_flags_status stat;
449 stat = dev_pm_qos_flags(pdd->dev, PM_QOS_FLAG_NO_POWER_OFF);
450 if (stat > PM_QOS_FLAGS_NONE)
451 return -EBUSY;
454 * Do not allow PM domain to be powered off, when an IRQ safe
455 * device is part of a non-IRQ safe domain.
457 if (!pm_runtime_suspended(pdd->dev) ||
458 irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
459 not_suspended++;
462 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
463 return -EBUSY;
465 if (genpd->gov && genpd->gov->power_down_ok) {
466 if (!genpd->gov->power_down_ok(&genpd->domain))
467 return -EAGAIN;
470 if (genpd->power_off) {
471 int ret;
473 if (atomic_read(&genpd->sd_count) > 0)
474 return -EBUSY;
477 * If sd_count > 0 at this point, one of the subdomains hasn't
478 * managed to call genpd_power_on() for the master yet after
479 * incrementing it. In that case genpd_power_on() will wait
480 * for us to drop the lock, so we can call .power_off() and let
481 * the genpd_power_on() restore power for us (this shouldn't
482 * happen very often).
484 ret = _genpd_power_off(genpd, true);
485 if (ret)
486 return ret;
489 genpd->status = GPD_STATE_POWER_OFF;
490 genpd_update_accounting(genpd);
492 list_for_each_entry(link, &genpd->slave_links, slave_node) {
493 genpd_sd_counter_dec(link->master);
494 genpd_lock_nested(link->master, depth + 1);
495 genpd_power_off(link->master, false, depth + 1);
496 genpd_unlock(link->master);
499 return 0;
503 * genpd_power_on - Restore power to a given PM domain and its masters.
504 * @genpd: PM domain to power up.
505 * @depth: nesting count for lockdep.
507 * Restore power to @genpd and all of its masters so that it is possible to
508 * resume a device belonging to it.
510 static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
512 struct gpd_link *link;
513 int ret = 0;
515 if (genpd_status_on(genpd))
516 return 0;
519 * The list is guaranteed not to change while the loop below is being
520 * executed, unless one of the masters' .power_on() callbacks fiddles
521 * with it.
523 list_for_each_entry(link, &genpd->slave_links, slave_node) {
524 struct generic_pm_domain *master = link->master;
526 genpd_sd_counter_inc(master);
528 genpd_lock_nested(master, depth + 1);
529 ret = genpd_power_on(master, depth + 1);
530 genpd_unlock(master);
532 if (ret) {
533 genpd_sd_counter_dec(master);
534 goto err;
538 ret = _genpd_power_on(genpd, true);
539 if (ret)
540 goto err;
542 genpd->status = GPD_STATE_ACTIVE;
543 genpd_update_accounting(genpd);
545 return 0;
547 err:
548 list_for_each_entry_continue_reverse(link,
549 &genpd->slave_links,
550 slave_node) {
551 genpd_sd_counter_dec(link->master);
552 genpd_lock_nested(link->master, depth + 1);
553 genpd_power_off(link->master, false, depth + 1);
554 genpd_unlock(link->master);
557 return ret;
560 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
561 unsigned long val, void *ptr)
563 struct generic_pm_domain_data *gpd_data;
564 struct device *dev;
566 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
567 dev = gpd_data->base.dev;
569 for (;;) {
570 struct generic_pm_domain *genpd;
571 struct pm_domain_data *pdd;
573 spin_lock_irq(&dev->power.lock);
575 pdd = dev->power.subsys_data ?
576 dev->power.subsys_data->domain_data : NULL;
577 if (pdd) {
578 to_gpd_data(pdd)->td.constraint_changed = true;
579 genpd = dev_to_genpd(dev);
580 } else {
581 genpd = ERR_PTR(-ENODATA);
584 spin_unlock_irq(&dev->power.lock);
586 if (!IS_ERR(genpd)) {
587 genpd_lock(genpd);
588 genpd->max_off_time_changed = true;
589 genpd_unlock(genpd);
592 dev = dev->parent;
593 if (!dev || dev->power.ignore_children)
594 break;
597 return NOTIFY_DONE;
601 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
602 * @work: Work structure used for scheduling the execution of this function.
604 static void genpd_power_off_work_fn(struct work_struct *work)
606 struct generic_pm_domain *genpd;
608 genpd = container_of(work, struct generic_pm_domain, power_off_work);
610 genpd_lock(genpd);
611 genpd_power_off(genpd, false, 0);
612 genpd_unlock(genpd);
616 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
617 * @dev: Device to handle.
619 static int __genpd_runtime_suspend(struct device *dev)
621 int (*cb)(struct device *__dev);
623 if (dev->type && dev->type->pm)
624 cb = dev->type->pm->runtime_suspend;
625 else if (dev->class && dev->class->pm)
626 cb = dev->class->pm->runtime_suspend;
627 else if (dev->bus && dev->bus->pm)
628 cb = dev->bus->pm->runtime_suspend;
629 else
630 cb = NULL;
632 if (!cb && dev->driver && dev->driver->pm)
633 cb = dev->driver->pm->runtime_suspend;
635 return cb ? cb(dev) : 0;
639 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
640 * @dev: Device to handle.
642 static int __genpd_runtime_resume(struct device *dev)
644 int (*cb)(struct device *__dev);
646 if (dev->type && dev->type->pm)
647 cb = dev->type->pm->runtime_resume;
648 else if (dev->class && dev->class->pm)
649 cb = dev->class->pm->runtime_resume;
650 else if (dev->bus && dev->bus->pm)
651 cb = dev->bus->pm->runtime_resume;
652 else
653 cb = NULL;
655 if (!cb && dev->driver && dev->driver->pm)
656 cb = dev->driver->pm->runtime_resume;
658 return cb ? cb(dev) : 0;
662 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
663 * @dev: Device to suspend.
665 * Carry out a runtime suspend of a device under the assumption that its
666 * pm_domain field points to the domain member of an object of type
667 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
669 static int genpd_runtime_suspend(struct device *dev)
671 struct generic_pm_domain *genpd;
672 bool (*suspend_ok)(struct device *__dev);
673 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
674 bool runtime_pm = pm_runtime_enabled(dev);
675 ktime_t time_start;
676 s64 elapsed_ns;
677 int ret;
679 dev_dbg(dev, "%s()\n", __func__);
681 genpd = dev_to_genpd(dev);
682 if (IS_ERR(genpd))
683 return -EINVAL;
686 * A runtime PM centric subsystem/driver may re-use the runtime PM
687 * callbacks for other purposes than runtime PM. In those scenarios
688 * runtime PM is disabled. Under these circumstances, we shall skip
689 * validating/measuring the PM QoS latency.
691 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
692 if (runtime_pm && suspend_ok && !suspend_ok(dev))
693 return -EBUSY;
695 /* Measure suspend latency. */
696 time_start = 0;
697 if (runtime_pm)
698 time_start = ktime_get();
700 ret = __genpd_runtime_suspend(dev);
701 if (ret)
702 return ret;
704 ret = genpd_stop_dev(genpd, dev);
705 if (ret) {
706 __genpd_runtime_resume(dev);
707 return ret;
710 /* Update suspend latency value if the measured time exceeds it. */
711 if (runtime_pm) {
712 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
713 if (elapsed_ns > td->suspend_latency_ns) {
714 td->suspend_latency_ns = elapsed_ns;
715 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
716 elapsed_ns);
717 genpd->max_off_time_changed = true;
718 td->constraint_changed = true;
723 * If power.irq_safe is set, this routine may be run with
724 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
726 if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
727 return 0;
729 genpd_lock(genpd);
730 genpd_power_off(genpd, true, 0);
731 genpd_unlock(genpd);
733 return 0;
737 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
738 * @dev: Device to resume.
740 * Carry out a runtime resume of a device under the assumption that its
741 * pm_domain field points to the domain member of an object of type
742 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
744 static int genpd_runtime_resume(struct device *dev)
746 struct generic_pm_domain *genpd;
747 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
748 bool runtime_pm = pm_runtime_enabled(dev);
749 ktime_t time_start;
750 s64 elapsed_ns;
751 int ret;
752 bool timed = true;
754 dev_dbg(dev, "%s()\n", __func__);
756 genpd = dev_to_genpd(dev);
757 if (IS_ERR(genpd))
758 return -EINVAL;
761 * As we don't power off a non IRQ safe domain, which holds
762 * an IRQ safe device, we don't need to restore power to it.
764 if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
765 timed = false;
766 goto out;
769 genpd_lock(genpd);
770 ret = genpd_power_on(genpd, 0);
771 genpd_unlock(genpd);
773 if (ret)
774 return ret;
776 out:
777 /* Measure resume latency. */
778 time_start = 0;
779 if (timed && runtime_pm)
780 time_start = ktime_get();
782 ret = genpd_start_dev(genpd, dev);
783 if (ret)
784 goto err_poweroff;
786 ret = __genpd_runtime_resume(dev);
787 if (ret)
788 goto err_stop;
790 /* Update resume latency value if the measured time exceeds it. */
791 if (timed && runtime_pm) {
792 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
793 if (elapsed_ns > td->resume_latency_ns) {
794 td->resume_latency_ns = elapsed_ns;
795 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
796 elapsed_ns);
797 genpd->max_off_time_changed = true;
798 td->constraint_changed = true;
802 return 0;
804 err_stop:
805 genpd_stop_dev(genpd, dev);
806 err_poweroff:
807 if (!pm_runtime_is_irq_safe(dev) ||
808 (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
809 genpd_lock(genpd);
810 genpd_power_off(genpd, true, 0);
811 genpd_unlock(genpd);
814 return ret;
817 static bool pd_ignore_unused;
818 static int __init pd_ignore_unused_setup(char *__unused)
820 pd_ignore_unused = true;
821 return 1;
823 __setup("pd_ignore_unused", pd_ignore_unused_setup);
826 * genpd_power_off_unused - Power off all PM domains with no devices in use.
828 static int __init genpd_power_off_unused(void)
830 struct generic_pm_domain *genpd;
832 if (pd_ignore_unused) {
833 pr_warn("genpd: Not disabling unused power domains\n");
834 return 0;
837 mutex_lock(&gpd_list_lock);
839 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
840 genpd_queue_power_off_work(genpd);
842 mutex_unlock(&gpd_list_lock);
844 return 0;
846 late_initcall(genpd_power_off_unused);
848 #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
850 static bool genpd_present(const struct generic_pm_domain *genpd)
852 const struct generic_pm_domain *gpd;
854 if (IS_ERR_OR_NULL(genpd))
855 return false;
857 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
858 if (gpd == genpd)
859 return true;
861 return false;
864 #endif
866 #ifdef CONFIG_PM_SLEEP
869 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
870 * @genpd: PM domain to power off, if possible.
871 * @use_lock: use the lock.
872 * @depth: nesting count for lockdep.
874 * Check if the given PM domain can be powered off (during system suspend or
875 * hibernation) and do that if so. Also, in that case propagate to its masters.
877 * This function is only called in "noirq" and "syscore" stages of system power
878 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
879 * these cases the lock must be held.
881 static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
882 unsigned int depth)
884 struct gpd_link *link;
886 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
887 return;
889 if (genpd->suspended_count != genpd->device_count
890 || atomic_read(&genpd->sd_count) > 0)
891 return;
893 /* Choose the deepest state when suspending */
894 genpd->state_idx = genpd->state_count - 1;
895 if (_genpd_power_off(genpd, false))
896 return;
898 genpd->status = GPD_STATE_POWER_OFF;
900 list_for_each_entry(link, &genpd->slave_links, slave_node) {
901 genpd_sd_counter_dec(link->master);
903 if (use_lock)
904 genpd_lock_nested(link->master, depth + 1);
906 genpd_sync_power_off(link->master, use_lock, depth + 1);
908 if (use_lock)
909 genpd_unlock(link->master);
914 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
915 * @genpd: PM domain to power on.
916 * @use_lock: use the lock.
917 * @depth: nesting count for lockdep.
919 * This function is only called in "noirq" and "syscore" stages of system power
920 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
921 * these cases the lock must be held.
923 static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
924 unsigned int depth)
926 struct gpd_link *link;
928 if (genpd_status_on(genpd))
929 return;
931 list_for_each_entry(link, &genpd->slave_links, slave_node) {
932 genpd_sd_counter_inc(link->master);
934 if (use_lock)
935 genpd_lock_nested(link->master, depth + 1);
937 genpd_sync_power_on(link->master, use_lock, depth + 1);
939 if (use_lock)
940 genpd_unlock(link->master);
943 _genpd_power_on(genpd, false);
945 genpd->status = GPD_STATE_ACTIVE;
949 * resume_needed - Check whether to resume a device before system suspend.
950 * @dev: Device to check.
951 * @genpd: PM domain the device belongs to.
953 * There are two cases in which a device that can wake up the system from sleep
954 * states should be resumed by genpd_prepare(): (1) if the device is enabled
955 * to wake up the system and it has to remain active for this purpose while the
956 * system is in the sleep state and (2) if the device is not enabled to wake up
957 * the system from sleep states and it generally doesn't generate wakeup signals
958 * by itself (those signals are generated on its behalf by other parts of the
959 * system). In the latter case it may be necessary to reconfigure the device's
960 * wakeup settings during system suspend, because it may have been set up to
961 * signal remote wakeup from the system's working state as needed by runtime PM.
962 * Return 'true' in either of the above cases.
964 static bool resume_needed(struct device *dev,
965 const struct generic_pm_domain *genpd)
967 bool active_wakeup;
969 if (!device_can_wakeup(dev))
970 return false;
972 active_wakeup = genpd_is_active_wakeup(genpd);
973 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
977 * genpd_prepare - Start power transition of a device in a PM domain.
978 * @dev: Device to start the transition of.
980 * Start a power transition of a device (during a system-wide power transition)
981 * under the assumption that its pm_domain field points to the domain member of
982 * an object of type struct generic_pm_domain representing a PM domain
983 * consisting of I/O devices.
985 static int genpd_prepare(struct device *dev)
987 struct generic_pm_domain *genpd;
988 int ret;
990 dev_dbg(dev, "%s()\n", __func__);
992 genpd = dev_to_genpd(dev);
993 if (IS_ERR(genpd))
994 return -EINVAL;
997 * If a wakeup request is pending for the device, it should be woken up
998 * at this point and a system wakeup event should be reported if it's
999 * set up to wake up the system from sleep states.
1001 if (resume_needed(dev, genpd))
1002 pm_runtime_resume(dev);
1004 genpd_lock(genpd);
1006 if (genpd->prepared_count++ == 0)
1007 genpd->suspended_count = 0;
1009 genpd_unlock(genpd);
1011 ret = pm_generic_prepare(dev);
1012 if (ret < 0) {
1013 genpd_lock(genpd);
1015 genpd->prepared_count--;
1017 genpd_unlock(genpd);
1020 /* Never return 1, as genpd don't cope with the direct_complete path. */
1021 return ret >= 0 ? 0 : ret;
1025 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1026 * I/O pm domain.
1027 * @dev: Device to suspend.
1028 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
1030 * Stop the device and remove power from the domain if all devices in it have
1031 * been stopped.
1033 static int genpd_finish_suspend(struct device *dev, bool poweroff)
1035 struct generic_pm_domain *genpd;
1036 int ret = 0;
1038 genpd = dev_to_genpd(dev);
1039 if (IS_ERR(genpd))
1040 return -EINVAL;
1042 if (poweroff)
1043 ret = pm_generic_poweroff_noirq(dev);
1044 else
1045 ret = pm_generic_suspend_noirq(dev);
1046 if (ret)
1047 return ret;
1049 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1050 return 0;
1052 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1053 !pm_runtime_status_suspended(dev)) {
1054 ret = genpd_stop_dev(genpd, dev);
1055 if (ret) {
1056 if (poweroff)
1057 pm_generic_restore_noirq(dev);
1058 else
1059 pm_generic_resume_noirq(dev);
1060 return ret;
1064 genpd_lock(genpd);
1065 genpd->suspended_count++;
1066 genpd_sync_power_off(genpd, true, 0);
1067 genpd_unlock(genpd);
1069 return 0;
1073 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1074 * @dev: Device to suspend.
1076 * Stop the device and remove power from the domain if all devices in it have
1077 * been stopped.
1079 static int genpd_suspend_noirq(struct device *dev)
1081 dev_dbg(dev, "%s()\n", __func__);
1083 return genpd_finish_suspend(dev, false);
1087 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1088 * @dev: Device to resume.
1090 * Restore power to the device's PM domain, if necessary, and start the device.
1092 static int genpd_resume_noirq(struct device *dev)
1094 struct generic_pm_domain *genpd;
1095 int ret;
1097 dev_dbg(dev, "%s()\n", __func__);
1099 genpd = dev_to_genpd(dev);
1100 if (IS_ERR(genpd))
1101 return -EINVAL;
1103 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1104 return pm_generic_resume_noirq(dev);
1106 genpd_lock(genpd);
1107 genpd_sync_power_on(genpd, true, 0);
1108 genpd->suspended_count--;
1109 genpd_unlock(genpd);
1111 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1112 !pm_runtime_status_suspended(dev)) {
1113 ret = genpd_start_dev(genpd, dev);
1114 if (ret)
1115 return ret;
1118 return pm_generic_resume_noirq(dev);
1122 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1123 * @dev: Device to freeze.
1125 * Carry out a late freeze of a device under the assumption that its
1126 * pm_domain field points to the domain member of an object of type
1127 * struct generic_pm_domain representing a power domain consisting of I/O
1128 * devices.
1130 static int genpd_freeze_noirq(struct device *dev)
1132 const struct generic_pm_domain *genpd;
1133 int ret = 0;
1135 dev_dbg(dev, "%s()\n", __func__);
1137 genpd = dev_to_genpd(dev);
1138 if (IS_ERR(genpd))
1139 return -EINVAL;
1141 ret = pm_generic_freeze_noirq(dev);
1142 if (ret)
1143 return ret;
1145 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1146 !pm_runtime_status_suspended(dev))
1147 ret = genpd_stop_dev(genpd, dev);
1149 return ret;
1153 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1154 * @dev: Device to thaw.
1156 * Start the device, unless power has been removed from the domain already
1157 * before the system transition.
1159 static int genpd_thaw_noirq(struct device *dev)
1161 const struct generic_pm_domain *genpd;
1162 int ret = 0;
1164 dev_dbg(dev, "%s()\n", __func__);
1166 genpd = dev_to_genpd(dev);
1167 if (IS_ERR(genpd))
1168 return -EINVAL;
1170 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1171 !pm_runtime_status_suspended(dev)) {
1172 ret = genpd_start_dev(genpd, dev);
1173 if (ret)
1174 return ret;
1177 return pm_generic_thaw_noirq(dev);
1181 * genpd_poweroff_noirq - Completion of hibernation of device in an
1182 * I/O PM domain.
1183 * @dev: Device to poweroff.
1185 * Stop the device and remove power from the domain if all devices in it have
1186 * been stopped.
1188 static int genpd_poweroff_noirq(struct device *dev)
1190 dev_dbg(dev, "%s()\n", __func__);
1192 return genpd_finish_suspend(dev, true);
1196 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1197 * @dev: Device to resume.
1199 * Make sure the domain will be in the same power state as before the
1200 * hibernation the system is resuming from and start the device if necessary.
1202 static int genpd_restore_noirq(struct device *dev)
1204 struct generic_pm_domain *genpd;
1205 int ret = 0;
1207 dev_dbg(dev, "%s()\n", __func__);
1209 genpd = dev_to_genpd(dev);
1210 if (IS_ERR(genpd))
1211 return -EINVAL;
1214 * At this point suspended_count == 0 means we are being run for the
1215 * first time for the given domain in the present cycle.
1217 genpd_lock(genpd);
1218 if (genpd->suspended_count++ == 0)
1220 * The boot kernel might put the domain into arbitrary state,
1221 * so make it appear as powered off to genpd_sync_power_on(),
1222 * so that it tries to power it on in case it was really off.
1224 genpd->status = GPD_STATE_POWER_OFF;
1226 genpd_sync_power_on(genpd, true, 0);
1227 genpd_unlock(genpd);
1229 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1230 !pm_runtime_status_suspended(dev)) {
1231 ret = genpd_start_dev(genpd, dev);
1232 if (ret)
1233 return ret;
1236 return pm_generic_restore_noirq(dev);
1240 * genpd_complete - Complete power transition of a device in a power domain.
1241 * @dev: Device to complete the transition of.
1243 * Complete a power transition of a device (during a system-wide power
1244 * transition) under the assumption that its pm_domain field points to the
1245 * domain member of an object of type struct generic_pm_domain representing
1246 * a power domain consisting of I/O devices.
1248 static void genpd_complete(struct device *dev)
1250 struct generic_pm_domain *genpd;
1252 dev_dbg(dev, "%s()\n", __func__);
1254 genpd = dev_to_genpd(dev);
1255 if (IS_ERR(genpd))
1256 return;
1258 pm_generic_complete(dev);
1260 genpd_lock(genpd);
1262 genpd->prepared_count--;
1263 if (!genpd->prepared_count)
1264 genpd_queue_power_off_work(genpd);
1266 genpd_unlock(genpd);
1270 * genpd_syscore_switch - Switch power during system core suspend or resume.
1271 * @dev: Device that normally is marked as "always on" to switch power for.
1273 * This routine may only be called during the system core (syscore) suspend or
1274 * resume phase for devices whose "always on" flags are set.
1276 static void genpd_syscore_switch(struct device *dev, bool suspend)
1278 struct generic_pm_domain *genpd;
1280 genpd = dev_to_genpd(dev);
1281 if (!genpd_present(genpd))
1282 return;
1284 if (suspend) {
1285 genpd->suspended_count++;
1286 genpd_sync_power_off(genpd, false, 0);
1287 } else {
1288 genpd_sync_power_on(genpd, false, 0);
1289 genpd->suspended_count--;
1293 void pm_genpd_syscore_poweroff(struct device *dev)
1295 genpd_syscore_switch(dev, true);
1297 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1299 void pm_genpd_syscore_poweron(struct device *dev)
1301 genpd_syscore_switch(dev, false);
1303 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1305 #else /* !CONFIG_PM_SLEEP */
1307 #define genpd_prepare NULL
1308 #define genpd_suspend_noirq NULL
1309 #define genpd_resume_noirq NULL
1310 #define genpd_freeze_noirq NULL
1311 #define genpd_thaw_noirq NULL
1312 #define genpd_poweroff_noirq NULL
1313 #define genpd_restore_noirq NULL
1314 #define genpd_complete NULL
1316 #endif /* CONFIG_PM_SLEEP */
1318 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1319 struct gpd_timing_data *td)
1321 struct generic_pm_domain_data *gpd_data;
1322 int ret;
1324 ret = dev_pm_get_subsys_data(dev);
1325 if (ret)
1326 return ERR_PTR(ret);
1328 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1329 if (!gpd_data) {
1330 ret = -ENOMEM;
1331 goto err_put;
1334 if (td)
1335 gpd_data->td = *td;
1337 gpd_data->base.dev = dev;
1338 gpd_data->td.constraint_changed = true;
1339 gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
1340 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1342 spin_lock_irq(&dev->power.lock);
1344 if (dev->power.subsys_data->domain_data) {
1345 ret = -EINVAL;
1346 goto err_free;
1349 dev->power.subsys_data->domain_data = &gpd_data->base;
1351 spin_unlock_irq(&dev->power.lock);
1353 return gpd_data;
1355 err_free:
1356 spin_unlock_irq(&dev->power.lock);
1357 kfree(gpd_data);
1358 err_put:
1359 dev_pm_put_subsys_data(dev);
1360 return ERR_PTR(ret);
1363 static void genpd_free_dev_data(struct device *dev,
1364 struct generic_pm_domain_data *gpd_data)
1366 spin_lock_irq(&dev->power.lock);
1368 dev->power.subsys_data->domain_data = NULL;
1370 spin_unlock_irq(&dev->power.lock);
1372 kfree(gpd_data);
1373 dev_pm_put_subsys_data(dev);
1376 static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1377 struct gpd_timing_data *td)
1379 struct generic_pm_domain_data *gpd_data;
1380 int ret;
1382 dev_dbg(dev, "%s()\n", __func__);
1384 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1385 return -EINVAL;
1387 gpd_data = genpd_alloc_dev_data(dev, td);
1388 if (IS_ERR(gpd_data))
1389 return PTR_ERR(gpd_data);
1391 genpd_lock(genpd);
1393 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1394 if (ret)
1395 goto out;
1397 dev_pm_domain_set(dev, &genpd->domain);
1399 genpd->device_count++;
1400 genpd->max_off_time_changed = true;
1402 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1404 out:
1405 genpd_unlock(genpd);
1407 if (ret)
1408 genpd_free_dev_data(dev, gpd_data);
1409 else
1410 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1412 return ret;
1416 * pm_genpd_add_device - Add a device to an I/O PM domain.
1417 * @genpd: PM domain to add the device to.
1418 * @dev: Device to be added.
1420 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1422 int ret;
1424 mutex_lock(&gpd_list_lock);
1425 ret = genpd_add_device(genpd, dev, NULL);
1426 mutex_unlock(&gpd_list_lock);
1428 return ret;
1430 EXPORT_SYMBOL_GPL(pm_genpd_add_device);
1432 static int genpd_remove_device(struct generic_pm_domain *genpd,
1433 struct device *dev)
1435 struct generic_pm_domain_data *gpd_data;
1436 struct pm_domain_data *pdd;
1437 int ret = 0;
1439 dev_dbg(dev, "%s()\n", __func__);
1441 pdd = dev->power.subsys_data->domain_data;
1442 gpd_data = to_gpd_data(pdd);
1443 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1445 genpd_lock(genpd);
1447 if (genpd->prepared_count > 0) {
1448 ret = -EAGAIN;
1449 goto out;
1452 genpd->device_count--;
1453 genpd->max_off_time_changed = true;
1455 if (genpd->detach_dev)
1456 genpd->detach_dev(genpd, dev);
1458 dev_pm_domain_set(dev, NULL);
1460 list_del_init(&pdd->list_node);
1462 genpd_unlock(genpd);
1464 genpd_free_dev_data(dev, gpd_data);
1466 return 0;
1468 out:
1469 genpd_unlock(genpd);
1470 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1472 return ret;
1476 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1477 * @dev: Device to be removed.
1479 int pm_genpd_remove_device(struct device *dev)
1481 struct generic_pm_domain *genpd = genpd_lookup_dev(dev);
1483 if (!genpd)
1484 return -EINVAL;
1486 return genpd_remove_device(genpd, dev);
1488 EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1490 static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1491 struct generic_pm_domain *subdomain)
1493 struct gpd_link *link, *itr;
1494 int ret = 0;
1496 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1497 || genpd == subdomain)
1498 return -EINVAL;
1501 * If the domain can be powered on/off in an IRQ safe
1502 * context, ensure that the subdomain can also be
1503 * powered on/off in that context.
1505 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1506 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1507 genpd->name, subdomain->name);
1508 return -EINVAL;
1511 link = kzalloc(sizeof(*link), GFP_KERNEL);
1512 if (!link)
1513 return -ENOMEM;
1515 genpd_lock(subdomain);
1516 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1518 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1519 ret = -EINVAL;
1520 goto out;
1523 list_for_each_entry(itr, &genpd->master_links, master_node) {
1524 if (itr->slave == subdomain && itr->master == genpd) {
1525 ret = -EINVAL;
1526 goto out;
1530 link->master = genpd;
1531 list_add_tail(&link->master_node, &genpd->master_links);
1532 link->slave = subdomain;
1533 list_add_tail(&link->slave_node, &subdomain->slave_links);
1534 if (genpd_status_on(subdomain))
1535 genpd_sd_counter_inc(genpd);
1537 out:
1538 genpd_unlock(genpd);
1539 genpd_unlock(subdomain);
1540 if (ret)
1541 kfree(link);
1542 return ret;
1546 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1547 * @genpd: Master PM domain to add the subdomain to.
1548 * @subdomain: Subdomain to be added.
1550 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1551 struct generic_pm_domain *subdomain)
1553 int ret;
1555 mutex_lock(&gpd_list_lock);
1556 ret = genpd_add_subdomain(genpd, subdomain);
1557 mutex_unlock(&gpd_list_lock);
1559 return ret;
1561 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1564 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1565 * @genpd: Master PM domain to remove the subdomain from.
1566 * @subdomain: Subdomain to be removed.
1568 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1569 struct generic_pm_domain *subdomain)
1571 struct gpd_link *l, *link;
1572 int ret = -EINVAL;
1574 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1575 return -EINVAL;
1577 genpd_lock(subdomain);
1578 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1580 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1581 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1582 subdomain->name);
1583 ret = -EBUSY;
1584 goto out;
1587 list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
1588 if (link->slave != subdomain)
1589 continue;
1591 list_del(&link->master_node);
1592 list_del(&link->slave_node);
1593 kfree(link);
1594 if (genpd_status_on(subdomain))
1595 genpd_sd_counter_dec(genpd);
1597 ret = 0;
1598 break;
1601 out:
1602 genpd_unlock(genpd);
1603 genpd_unlock(subdomain);
1605 return ret;
1607 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1609 static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1611 struct genpd_power_state *state;
1613 state = kzalloc(sizeof(*state), GFP_KERNEL);
1614 if (!state)
1615 return -ENOMEM;
1617 genpd->states = state;
1618 genpd->state_count = 1;
1619 genpd->free = state;
1621 return 0;
1624 static void genpd_lock_init(struct generic_pm_domain *genpd)
1626 if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1627 spin_lock_init(&genpd->slock);
1628 genpd->lock_ops = &genpd_spin_ops;
1629 } else {
1630 mutex_init(&genpd->mlock);
1631 genpd->lock_ops = &genpd_mtx_ops;
1636 * pm_genpd_init - Initialize a generic I/O PM domain object.
1637 * @genpd: PM domain object to initialize.
1638 * @gov: PM domain governor to associate with the domain (may be NULL).
1639 * @is_off: Initial value of the domain's power_is_off field.
1641 * Returns 0 on successful initialization, else a negative error code.
1643 int pm_genpd_init(struct generic_pm_domain *genpd,
1644 struct dev_power_governor *gov, bool is_off)
1646 int ret;
1648 if (IS_ERR_OR_NULL(genpd))
1649 return -EINVAL;
1651 INIT_LIST_HEAD(&genpd->master_links);
1652 INIT_LIST_HEAD(&genpd->slave_links);
1653 INIT_LIST_HEAD(&genpd->dev_list);
1654 genpd_lock_init(genpd);
1655 genpd->gov = gov;
1656 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1657 atomic_set(&genpd->sd_count, 0);
1658 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1659 genpd->device_count = 0;
1660 genpd->max_off_time_ns = -1;
1661 genpd->max_off_time_changed = true;
1662 genpd->provider = NULL;
1663 genpd->has_provider = false;
1664 genpd->accounting_time = ktime_get();
1665 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1666 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
1667 genpd->domain.ops.prepare = genpd_prepare;
1668 genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
1669 genpd->domain.ops.resume_noirq = genpd_resume_noirq;
1670 genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
1671 genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
1672 genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
1673 genpd->domain.ops.restore_noirq = genpd_restore_noirq;
1674 genpd->domain.ops.complete = genpd_complete;
1676 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1677 genpd->dev_ops.stop = pm_clk_suspend;
1678 genpd->dev_ops.start = pm_clk_resume;
1681 /* Always-on domains must be powered on at initialization. */
1682 if (genpd_is_always_on(genpd) && !genpd_status_on(genpd))
1683 return -EINVAL;
1685 /* Use only one "off" state if there were no states declared */
1686 if (genpd->state_count == 0) {
1687 ret = genpd_set_default_power_state(genpd);
1688 if (ret)
1689 return ret;
1692 device_initialize(&genpd->dev);
1693 dev_set_name(&genpd->dev, "%s", genpd->name);
1695 mutex_lock(&gpd_list_lock);
1696 list_add(&genpd->gpd_list_node, &gpd_list);
1697 mutex_unlock(&gpd_list_lock);
1699 return 0;
1701 EXPORT_SYMBOL_GPL(pm_genpd_init);
1703 static int genpd_remove(struct generic_pm_domain *genpd)
1705 struct gpd_link *l, *link;
1707 if (IS_ERR_OR_NULL(genpd))
1708 return -EINVAL;
1710 genpd_lock(genpd);
1712 if (genpd->has_provider) {
1713 genpd_unlock(genpd);
1714 pr_err("Provider present, unable to remove %s\n", genpd->name);
1715 return -EBUSY;
1718 if (!list_empty(&genpd->master_links) || genpd->device_count) {
1719 genpd_unlock(genpd);
1720 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1721 return -EBUSY;
1724 list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
1725 list_del(&link->master_node);
1726 list_del(&link->slave_node);
1727 kfree(link);
1730 list_del(&genpd->gpd_list_node);
1731 genpd_unlock(genpd);
1732 cancel_work_sync(&genpd->power_off_work);
1733 kfree(genpd->free);
1734 pr_debug("%s: removed %s\n", __func__, genpd->name);
1736 return 0;
1740 * pm_genpd_remove - Remove a generic I/O PM domain
1741 * @genpd: Pointer to PM domain that is to be removed.
1743 * To remove the PM domain, this function:
1744 * - Removes the PM domain as a subdomain to any parent domains,
1745 * if it was added.
1746 * - Removes the PM domain from the list of registered PM domains.
1748 * The PM domain will only be removed, if the associated provider has
1749 * been removed, it is not a parent to any other PM domain and has no
1750 * devices associated with it.
1752 int pm_genpd_remove(struct generic_pm_domain *genpd)
1754 int ret;
1756 mutex_lock(&gpd_list_lock);
1757 ret = genpd_remove(genpd);
1758 mutex_unlock(&gpd_list_lock);
1760 return ret;
1762 EXPORT_SYMBOL_GPL(pm_genpd_remove);
1764 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1767 * Device Tree based PM domain providers.
1769 * The code below implements generic device tree based PM domain providers that
1770 * bind device tree nodes with generic PM domains registered in the system.
1772 * Any driver that registers generic PM domains and needs to support binding of
1773 * devices to these domains is supposed to register a PM domain provider, which
1774 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1776 * Two simple mapping functions have been provided for convenience:
1777 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1778 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1779 * index.
1783 * struct of_genpd_provider - PM domain provider registration structure
1784 * @link: Entry in global list of PM domain providers
1785 * @node: Pointer to device tree node of PM domain provider
1786 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1787 * into a PM domain.
1788 * @data: context pointer to be passed into @xlate callback
1790 struct of_genpd_provider {
1791 struct list_head link;
1792 struct device_node *node;
1793 genpd_xlate_t xlate;
1794 void *data;
1797 /* List of registered PM domain providers. */
1798 static LIST_HEAD(of_genpd_providers);
1799 /* Mutex to protect the list above. */
1800 static DEFINE_MUTEX(of_genpd_mutex);
1803 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1804 * @genpdspec: OF phandle args to map into a PM domain
1805 * @data: xlate function private data - pointer to struct generic_pm_domain
1807 * This is a generic xlate function that can be used to model PM domains that
1808 * have their own device tree nodes. The private data of xlate function needs
1809 * to be a valid pointer to struct generic_pm_domain.
1811 static struct generic_pm_domain *genpd_xlate_simple(
1812 struct of_phandle_args *genpdspec,
1813 void *data)
1815 return data;
1819 * genpd_xlate_onecell() - Xlate function using a single index.
1820 * @genpdspec: OF phandle args to map into a PM domain
1821 * @data: xlate function private data - pointer to struct genpd_onecell_data
1823 * This is a generic xlate function that can be used to model simple PM domain
1824 * controllers that have one device tree node and provide multiple PM domains.
1825 * A single cell is used as an index into an array of PM domains specified in
1826 * the genpd_onecell_data struct when registering the provider.
1828 static struct generic_pm_domain *genpd_xlate_onecell(
1829 struct of_phandle_args *genpdspec,
1830 void *data)
1832 struct genpd_onecell_data *genpd_data = data;
1833 unsigned int idx = genpdspec->args[0];
1835 if (genpdspec->args_count != 1)
1836 return ERR_PTR(-EINVAL);
1838 if (idx >= genpd_data->num_domains) {
1839 pr_err("%s: invalid domain index %u\n", __func__, idx);
1840 return ERR_PTR(-EINVAL);
1843 if (!genpd_data->domains[idx])
1844 return ERR_PTR(-ENOENT);
1846 return genpd_data->domains[idx];
1850 * genpd_add_provider() - Register a PM domain provider for a node
1851 * @np: Device node pointer associated with the PM domain provider.
1852 * @xlate: Callback for decoding PM domain from phandle arguments.
1853 * @data: Context pointer for @xlate callback.
1855 static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1856 void *data)
1858 struct of_genpd_provider *cp;
1860 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1861 if (!cp)
1862 return -ENOMEM;
1864 cp->node = of_node_get(np);
1865 cp->data = data;
1866 cp->xlate = xlate;
1868 mutex_lock(&of_genpd_mutex);
1869 list_add(&cp->link, &of_genpd_providers);
1870 mutex_unlock(&of_genpd_mutex);
1871 pr_debug("Added domain provider from %pOF\n", np);
1873 return 0;
1877 * of_genpd_add_provider_simple() - Register a simple PM domain provider
1878 * @np: Device node pointer associated with the PM domain provider.
1879 * @genpd: Pointer to PM domain associated with the PM domain provider.
1881 int of_genpd_add_provider_simple(struct device_node *np,
1882 struct generic_pm_domain *genpd)
1884 int ret = -EINVAL;
1886 if (!np || !genpd)
1887 return -EINVAL;
1889 mutex_lock(&gpd_list_lock);
1891 if (!genpd_present(genpd))
1892 goto unlock;
1894 genpd->dev.of_node = np;
1896 /* Parse genpd OPP table */
1897 if (genpd->set_performance_state) {
1898 ret = dev_pm_opp_of_add_table(&genpd->dev);
1899 if (ret) {
1900 dev_err(&genpd->dev, "Failed to add OPP table: %d\n",
1901 ret);
1902 goto unlock;
1906 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
1907 if (ret) {
1908 if (genpd->set_performance_state)
1909 dev_pm_opp_of_remove_table(&genpd->dev);
1911 goto unlock;
1914 genpd->provider = &np->fwnode;
1915 genpd->has_provider = true;
1917 unlock:
1918 mutex_unlock(&gpd_list_lock);
1920 return ret;
1922 EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
1925 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
1926 * @np: Device node pointer associated with the PM domain provider.
1927 * @data: Pointer to the data associated with the PM domain provider.
1929 int of_genpd_add_provider_onecell(struct device_node *np,
1930 struct genpd_onecell_data *data)
1932 struct generic_pm_domain *genpd;
1933 unsigned int i;
1934 int ret = -EINVAL;
1936 if (!np || !data)
1937 return -EINVAL;
1939 mutex_lock(&gpd_list_lock);
1941 if (!data->xlate)
1942 data->xlate = genpd_xlate_onecell;
1944 for (i = 0; i < data->num_domains; i++) {
1945 genpd = data->domains[i];
1947 if (!genpd)
1948 continue;
1949 if (!genpd_present(genpd))
1950 goto error;
1952 genpd->dev.of_node = np;
1954 /* Parse genpd OPP table */
1955 if (genpd->set_performance_state) {
1956 ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
1957 if (ret) {
1958 dev_err(&genpd->dev, "Failed to add OPP table for index %d: %d\n",
1959 i, ret);
1960 goto error;
1964 genpd->provider = &np->fwnode;
1965 genpd->has_provider = true;
1968 ret = genpd_add_provider(np, data->xlate, data);
1969 if (ret < 0)
1970 goto error;
1972 mutex_unlock(&gpd_list_lock);
1974 return 0;
1976 error:
1977 while (i--) {
1978 genpd = data->domains[i];
1980 if (!genpd)
1981 continue;
1983 genpd->provider = NULL;
1984 genpd->has_provider = false;
1986 if (genpd->set_performance_state)
1987 dev_pm_opp_of_remove_table(&genpd->dev);
1990 mutex_unlock(&gpd_list_lock);
1992 return ret;
1994 EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
1997 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1998 * @np: Device node pointer associated with the PM domain provider
2000 void of_genpd_del_provider(struct device_node *np)
2002 struct of_genpd_provider *cp, *tmp;
2003 struct generic_pm_domain *gpd;
2005 mutex_lock(&gpd_list_lock);
2006 mutex_lock(&of_genpd_mutex);
2007 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
2008 if (cp->node == np) {
2010 * For each PM domain associated with the
2011 * provider, set the 'has_provider' to false
2012 * so that the PM domain can be safely removed.
2014 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2015 if (gpd->provider == &np->fwnode) {
2016 gpd->has_provider = false;
2018 if (!gpd->set_performance_state)
2019 continue;
2021 dev_pm_opp_of_remove_table(&gpd->dev);
2025 list_del(&cp->link);
2026 of_node_put(cp->node);
2027 kfree(cp);
2028 break;
2031 mutex_unlock(&of_genpd_mutex);
2032 mutex_unlock(&gpd_list_lock);
2034 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2037 * genpd_get_from_provider() - Look-up PM domain
2038 * @genpdspec: OF phandle args to use for look-up
2040 * Looks for a PM domain provider under the node specified by @genpdspec and if
2041 * found, uses xlate function of the provider to map phandle args to a PM
2042 * domain.
2044 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2045 * on failure.
2047 static struct generic_pm_domain *genpd_get_from_provider(
2048 struct of_phandle_args *genpdspec)
2050 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2051 struct of_genpd_provider *provider;
2053 if (!genpdspec)
2054 return ERR_PTR(-EINVAL);
2056 mutex_lock(&of_genpd_mutex);
2058 /* Check if we have such a provider in our array */
2059 list_for_each_entry(provider, &of_genpd_providers, link) {
2060 if (provider->node == genpdspec->np)
2061 genpd = provider->xlate(genpdspec, provider->data);
2062 if (!IS_ERR(genpd))
2063 break;
2066 mutex_unlock(&of_genpd_mutex);
2068 return genpd;
2072 * of_genpd_add_device() - Add a device to an I/O PM domain
2073 * @genpdspec: OF phandle args to use for look-up PM domain
2074 * @dev: Device to be added.
2076 * Looks-up an I/O PM domain based upon phandle args provided and adds
2077 * the device to the PM domain. Returns a negative error code on failure.
2079 int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
2081 struct generic_pm_domain *genpd;
2082 int ret;
2084 mutex_lock(&gpd_list_lock);
2086 genpd = genpd_get_from_provider(genpdspec);
2087 if (IS_ERR(genpd)) {
2088 ret = PTR_ERR(genpd);
2089 goto out;
2092 ret = genpd_add_device(genpd, dev, NULL);
2094 out:
2095 mutex_unlock(&gpd_list_lock);
2097 return ret;
2099 EXPORT_SYMBOL_GPL(of_genpd_add_device);
2102 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2103 * @parent_spec: OF phandle args to use for parent PM domain look-up
2104 * @subdomain_spec: OF phandle args to use for subdomain look-up
2106 * Looks-up a parent PM domain and subdomain based upon phandle args
2107 * provided and adds the subdomain to the parent PM domain. Returns a
2108 * negative error code on failure.
2110 int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
2111 struct of_phandle_args *subdomain_spec)
2113 struct generic_pm_domain *parent, *subdomain;
2114 int ret;
2116 mutex_lock(&gpd_list_lock);
2118 parent = genpd_get_from_provider(parent_spec);
2119 if (IS_ERR(parent)) {
2120 ret = PTR_ERR(parent);
2121 goto out;
2124 subdomain = genpd_get_from_provider(subdomain_spec);
2125 if (IS_ERR(subdomain)) {
2126 ret = PTR_ERR(subdomain);
2127 goto out;
2130 ret = genpd_add_subdomain(parent, subdomain);
2132 out:
2133 mutex_unlock(&gpd_list_lock);
2135 return ret;
2137 EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2140 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2141 * @provider: Pointer to device structure associated with provider
2143 * Find the last PM domain that was added by a particular provider and
2144 * remove this PM domain from the list of PM domains. The provider is
2145 * identified by the 'provider' device structure that is passed. The PM
2146 * domain will only be removed, if the provider associated with domain
2147 * has been removed.
2149 * Returns a valid pointer to struct generic_pm_domain on success or
2150 * ERR_PTR() on failure.
2152 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2154 struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2155 int ret;
2157 if (IS_ERR_OR_NULL(np))
2158 return ERR_PTR(-EINVAL);
2160 mutex_lock(&gpd_list_lock);
2161 list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2162 if (gpd->provider == &np->fwnode) {
2163 ret = genpd_remove(gpd);
2164 genpd = ret ? ERR_PTR(ret) : gpd;
2165 break;
2168 mutex_unlock(&gpd_list_lock);
2170 return genpd;
2172 EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2174 static void genpd_release_dev(struct device *dev)
2176 kfree(dev);
2179 static struct bus_type genpd_bus_type = {
2180 .name = "genpd",
2184 * genpd_dev_pm_detach - Detach a device from its PM domain.
2185 * @dev: Device to detach.
2186 * @power_off: Currently not used
2188 * Try to locate a corresponding generic PM domain, which the device was
2189 * attached to previously. If such is found, the device is detached from it.
2191 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2193 struct generic_pm_domain *pd;
2194 unsigned int i;
2195 int ret = 0;
2197 pd = dev_to_genpd(dev);
2198 if (IS_ERR(pd))
2199 return;
2201 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2203 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2204 ret = genpd_remove_device(pd, dev);
2205 if (ret != -EAGAIN)
2206 break;
2208 mdelay(i);
2209 cond_resched();
2212 if (ret < 0) {
2213 dev_err(dev, "failed to remove from PM domain %s: %d",
2214 pd->name, ret);
2215 return;
2218 /* Check if PM domain can be powered off after removing this device. */
2219 genpd_queue_power_off_work(pd);
2221 /* Unregister the device if it was created by genpd. */
2222 if (dev->bus == &genpd_bus_type)
2223 device_unregister(dev);
2226 static void genpd_dev_pm_sync(struct device *dev)
2228 struct generic_pm_domain *pd;
2230 pd = dev_to_genpd(dev);
2231 if (IS_ERR(pd))
2232 return;
2234 genpd_queue_power_off_work(pd);
2237 static int __genpd_dev_pm_attach(struct device *dev, struct device_node *np,
2238 unsigned int index, bool power_on)
2240 struct of_phandle_args pd_args;
2241 struct generic_pm_domain *pd;
2242 int ret;
2244 ret = of_parse_phandle_with_args(np, "power-domains",
2245 "#power-domain-cells", index, &pd_args);
2246 if (ret < 0)
2247 return ret;
2249 mutex_lock(&gpd_list_lock);
2250 pd = genpd_get_from_provider(&pd_args);
2251 of_node_put(pd_args.np);
2252 if (IS_ERR(pd)) {
2253 mutex_unlock(&gpd_list_lock);
2254 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2255 __func__, PTR_ERR(pd));
2256 return driver_deferred_probe_check_state(dev);
2259 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2261 ret = genpd_add_device(pd, dev, NULL);
2262 mutex_unlock(&gpd_list_lock);
2264 if (ret < 0) {
2265 if (ret != -EPROBE_DEFER)
2266 dev_err(dev, "failed to add to PM domain %s: %d",
2267 pd->name, ret);
2268 return ret;
2271 dev->pm_domain->detach = genpd_dev_pm_detach;
2272 dev->pm_domain->sync = genpd_dev_pm_sync;
2274 if (power_on) {
2275 genpd_lock(pd);
2276 ret = genpd_power_on(pd, 0);
2277 genpd_unlock(pd);
2280 if (ret)
2281 genpd_remove_device(pd, dev);
2283 return ret ? -EPROBE_DEFER : 1;
2287 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2288 * @dev: Device to attach.
2290 * Parse device's OF node to find a PM domain specifier. If such is found,
2291 * attaches the device to retrieved pm_domain ops.
2293 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2294 * PM domain or when multiple power-domains exists for it, else a negative error
2295 * code. Note that if a power-domain exists for the device, but it cannot be
2296 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2297 * not probed and to re-try again later.
2299 int genpd_dev_pm_attach(struct device *dev)
2301 if (!dev->of_node)
2302 return 0;
2305 * Devices with multiple PM domains must be attached separately, as we
2306 * can only attach one PM domain per device.
2308 if (of_count_phandle_with_args(dev->of_node, "power-domains",
2309 "#power-domain-cells") != 1)
2310 return 0;
2312 return __genpd_dev_pm_attach(dev, dev->of_node, 0, true);
2314 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2317 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2318 * @dev: The device used to lookup the PM domain.
2319 * @index: The index of the PM domain.
2321 * Parse device's OF node to find a PM domain specifier at the provided @index.
2322 * If such is found, creates a virtual device and attaches it to the retrieved
2323 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2324 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2326 * Returns the created virtual device if successfully attached PM domain, NULL
2327 * when the device don't need a PM domain, else an ERR_PTR() in case of
2328 * failures. If a power-domain exists for the device, but cannot be found or
2329 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2330 * is not probed and to re-try again later.
2332 struct device *genpd_dev_pm_attach_by_id(struct device *dev,
2333 unsigned int index)
2335 struct device *genpd_dev;
2336 int num_domains;
2337 int ret;
2339 if (!dev->of_node)
2340 return NULL;
2342 /* Deal only with devices using multiple PM domains. */
2343 num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
2344 "#power-domain-cells");
2345 if (num_domains < 2 || index >= num_domains)
2346 return NULL;
2348 /* Allocate and register device on the genpd bus. */
2349 genpd_dev = kzalloc(sizeof(*genpd_dev), GFP_KERNEL);
2350 if (!genpd_dev)
2351 return ERR_PTR(-ENOMEM);
2353 dev_set_name(genpd_dev, "genpd:%u:%s", index, dev_name(dev));
2354 genpd_dev->bus = &genpd_bus_type;
2355 genpd_dev->release = genpd_release_dev;
2357 ret = device_register(genpd_dev);
2358 if (ret) {
2359 kfree(genpd_dev);
2360 return ERR_PTR(ret);
2363 /* Try to attach the device to the PM domain at the specified index. */
2364 ret = __genpd_dev_pm_attach(genpd_dev, dev->of_node, index, false);
2365 if (ret < 1) {
2366 device_unregister(genpd_dev);
2367 return ret ? ERR_PTR(ret) : NULL;
2370 pm_runtime_enable(genpd_dev);
2371 genpd_queue_power_off_work(dev_to_genpd(genpd_dev));
2373 return genpd_dev;
2375 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
2378 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
2379 * @dev: The device used to lookup the PM domain.
2380 * @name: The name of the PM domain.
2382 * Parse device's OF node to find a PM domain specifier using the
2383 * power-domain-names DT property. For further description see
2384 * genpd_dev_pm_attach_by_id().
2386 struct device *genpd_dev_pm_attach_by_name(struct device *dev, char *name)
2388 int index;
2390 if (!dev->of_node)
2391 return NULL;
2393 index = of_property_match_string(dev->of_node, "power-domain-names",
2394 name);
2395 if (index < 0)
2396 return NULL;
2398 return genpd_dev_pm_attach_by_id(dev, index);
2401 static const struct of_device_id idle_state_match[] = {
2402 { .compatible = "domain-idle-state", },
2406 static int genpd_parse_state(struct genpd_power_state *genpd_state,
2407 struct device_node *state_node)
2409 int err;
2410 u32 residency;
2411 u32 entry_latency, exit_latency;
2413 err = of_property_read_u32(state_node, "entry-latency-us",
2414 &entry_latency);
2415 if (err) {
2416 pr_debug(" * %pOF missing entry-latency-us property\n",
2417 state_node);
2418 return -EINVAL;
2421 err = of_property_read_u32(state_node, "exit-latency-us",
2422 &exit_latency);
2423 if (err) {
2424 pr_debug(" * %pOF missing exit-latency-us property\n",
2425 state_node);
2426 return -EINVAL;
2429 err = of_property_read_u32(state_node, "min-residency-us", &residency);
2430 if (!err)
2431 genpd_state->residency_ns = 1000 * residency;
2433 genpd_state->power_on_latency_ns = 1000 * exit_latency;
2434 genpd_state->power_off_latency_ns = 1000 * entry_latency;
2435 genpd_state->fwnode = &state_node->fwnode;
2437 return 0;
2440 static int genpd_iterate_idle_states(struct device_node *dn,
2441 struct genpd_power_state *states)
2443 int ret;
2444 struct of_phandle_iterator it;
2445 struct device_node *np;
2446 int i = 0;
2448 ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2449 if (ret <= 0)
2450 return ret;
2452 /* Loop over the phandles until all the requested entry is found */
2453 of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
2454 np = it.node;
2455 if (!of_match_node(idle_state_match, np))
2456 continue;
2457 if (states) {
2458 ret = genpd_parse_state(&states[i], np);
2459 if (ret) {
2460 pr_err("Parsing idle state node %pOF failed with err %d\n",
2461 np, ret);
2462 of_node_put(np);
2463 return ret;
2466 i++;
2469 return i;
2473 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2475 * @dn: The genpd device node
2476 * @states: The pointer to which the state array will be saved.
2477 * @n: The count of elements in the array returned from this function.
2479 * Returns the device states parsed from the OF node. The memory for the states
2480 * is allocated by this function and is the responsibility of the caller to
2481 * free the memory after use. If no domain idle states is found it returns
2482 * -EINVAL and in case of errors, a negative error code.
2484 int of_genpd_parse_idle_states(struct device_node *dn,
2485 struct genpd_power_state **states, int *n)
2487 struct genpd_power_state *st;
2488 int ret;
2490 ret = genpd_iterate_idle_states(dn, NULL);
2491 if (ret <= 0)
2492 return ret < 0 ? ret : -EINVAL;
2494 st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
2495 if (!st)
2496 return -ENOMEM;
2498 ret = genpd_iterate_idle_states(dn, st);
2499 if (ret <= 0) {
2500 kfree(st);
2501 return ret < 0 ? ret : -EINVAL;
2504 *states = st;
2505 *n = ret;
2507 return 0;
2509 EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2512 * of_genpd_opp_to_performance_state- Gets performance state of device's
2513 * power domain corresponding to a DT node's "required-opps" property.
2515 * @dev: Device for which the performance-state needs to be found.
2516 * @np: DT node where the "required-opps" property is present. This can be
2517 * the device node itself (if it doesn't have an OPP table) or a node
2518 * within the OPP table of a device (if device has an OPP table).
2520 * Returns performance state corresponding to the "required-opps" property of
2521 * a DT node. This calls platform specific genpd->opp_to_performance_state()
2522 * callback to translate power domain OPP to performance state.
2524 * Returns performance state on success and 0 on failure.
2526 unsigned int of_genpd_opp_to_performance_state(struct device *dev,
2527 struct device_node *np)
2529 struct generic_pm_domain *genpd;
2530 struct dev_pm_opp *opp;
2531 int state = 0;
2533 genpd = dev_to_genpd(dev);
2534 if (IS_ERR(genpd))
2535 return 0;
2537 if (unlikely(!genpd->set_performance_state))
2538 return 0;
2540 genpd_lock(genpd);
2542 opp = of_dev_pm_opp_find_required_opp(&genpd->dev, np);
2543 if (IS_ERR(opp)) {
2544 dev_err(dev, "Failed to find required OPP: %ld\n",
2545 PTR_ERR(opp));
2546 goto unlock;
2549 state = genpd->opp_to_performance_state(genpd, opp);
2550 dev_pm_opp_put(opp);
2552 unlock:
2553 genpd_unlock(genpd);
2555 return state;
2557 EXPORT_SYMBOL_GPL(of_genpd_opp_to_performance_state);
2559 static int __init genpd_bus_init(void)
2561 return bus_register(&genpd_bus_type);
2563 core_initcall(genpd_bus_init);
2565 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2568 /*** debugfs support ***/
2570 #ifdef CONFIG_DEBUG_FS
2571 #include <linux/pm.h>
2572 #include <linux/device.h>
2573 #include <linux/debugfs.h>
2574 #include <linux/seq_file.h>
2575 #include <linux/init.h>
2576 #include <linux/kobject.h>
2577 static struct dentry *genpd_debugfs_dir;
2580 * TODO: This function is a slightly modified version of rtpm_status_show
2581 * from sysfs.c, so generalize it.
2583 static void rtpm_status_str(struct seq_file *s, struct device *dev)
2585 static const char * const status_lookup[] = {
2586 [RPM_ACTIVE] = "active",
2587 [RPM_RESUMING] = "resuming",
2588 [RPM_SUSPENDED] = "suspended",
2589 [RPM_SUSPENDING] = "suspending"
2591 const char *p = "";
2593 if (dev->power.runtime_error)
2594 p = "error";
2595 else if (dev->power.disable_depth)
2596 p = "unsupported";
2597 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2598 p = status_lookup[dev->power.runtime_status];
2599 else
2600 WARN_ON(1);
2602 seq_puts(s, p);
2605 static int genpd_summary_one(struct seq_file *s,
2606 struct generic_pm_domain *genpd)
2608 static const char * const status_lookup[] = {
2609 [GPD_STATE_ACTIVE] = "on",
2610 [GPD_STATE_POWER_OFF] = "off"
2612 struct pm_domain_data *pm_data;
2613 const char *kobj_path;
2614 struct gpd_link *link;
2615 char state[16];
2616 int ret;
2618 ret = genpd_lock_interruptible(genpd);
2619 if (ret)
2620 return -ERESTARTSYS;
2622 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2623 goto exit;
2624 if (!genpd_status_on(genpd))
2625 snprintf(state, sizeof(state), "%s-%u",
2626 status_lookup[genpd->status], genpd->state_idx);
2627 else
2628 snprintf(state, sizeof(state), "%s",
2629 status_lookup[genpd->status]);
2630 seq_printf(s, "%-30s %-15s ", genpd->name, state);
2633 * Modifications on the list require holding locks on both
2634 * master and slave, so we are safe.
2635 * Also genpd->name is immutable.
2637 list_for_each_entry(link, &genpd->master_links, master_node) {
2638 seq_printf(s, "%s", link->slave->name);
2639 if (!list_is_last(&link->master_node, &genpd->master_links))
2640 seq_puts(s, ", ");
2643 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2644 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2645 genpd_is_irq_safe(genpd) ?
2646 GFP_ATOMIC : GFP_KERNEL);
2647 if (kobj_path == NULL)
2648 continue;
2650 seq_printf(s, "\n %-50s ", kobj_path);
2651 rtpm_status_str(s, pm_data->dev);
2652 kfree(kobj_path);
2655 seq_puts(s, "\n");
2656 exit:
2657 genpd_unlock(genpd);
2659 return 0;
2662 static int genpd_summary_show(struct seq_file *s, void *data)
2664 struct generic_pm_domain *genpd;
2665 int ret = 0;
2667 seq_puts(s, "domain status slaves\n");
2668 seq_puts(s, " /device runtime status\n");
2669 seq_puts(s, "----------------------------------------------------------------------\n");
2671 ret = mutex_lock_interruptible(&gpd_list_lock);
2672 if (ret)
2673 return -ERESTARTSYS;
2675 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2676 ret = genpd_summary_one(s, genpd);
2677 if (ret)
2678 break;
2680 mutex_unlock(&gpd_list_lock);
2682 return ret;
2685 static int genpd_status_show(struct seq_file *s, void *data)
2687 static const char * const status_lookup[] = {
2688 [GPD_STATE_ACTIVE] = "on",
2689 [GPD_STATE_POWER_OFF] = "off"
2692 struct generic_pm_domain *genpd = s->private;
2693 int ret = 0;
2695 ret = genpd_lock_interruptible(genpd);
2696 if (ret)
2697 return -ERESTARTSYS;
2699 if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
2700 goto exit;
2702 if (genpd->status == GPD_STATE_POWER_OFF)
2703 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
2704 genpd->state_idx);
2705 else
2706 seq_printf(s, "%s\n", status_lookup[genpd->status]);
2707 exit:
2708 genpd_unlock(genpd);
2709 return ret;
2712 static int genpd_sub_domains_show(struct seq_file *s, void *data)
2714 struct generic_pm_domain *genpd = s->private;
2715 struct gpd_link *link;
2716 int ret = 0;
2718 ret = genpd_lock_interruptible(genpd);
2719 if (ret)
2720 return -ERESTARTSYS;
2722 list_for_each_entry(link, &genpd->master_links, master_node)
2723 seq_printf(s, "%s\n", link->slave->name);
2725 genpd_unlock(genpd);
2726 return ret;
2729 static int genpd_idle_states_show(struct seq_file *s, void *data)
2731 struct generic_pm_domain *genpd = s->private;
2732 unsigned int i;
2733 int ret = 0;
2735 ret = genpd_lock_interruptible(genpd);
2736 if (ret)
2737 return -ERESTARTSYS;
2739 seq_puts(s, "State Time Spent(ms)\n");
2741 for (i = 0; i < genpd->state_count; i++) {
2742 ktime_t delta = 0;
2743 s64 msecs;
2745 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2746 (genpd->state_idx == i))
2747 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2749 msecs = ktime_to_ms(
2750 ktime_add(genpd->states[i].idle_time, delta));
2751 seq_printf(s, "S%-13i %lld\n", i, msecs);
2754 genpd_unlock(genpd);
2755 return ret;
2758 static int genpd_active_time_show(struct seq_file *s, void *data)
2760 struct generic_pm_domain *genpd = s->private;
2761 ktime_t delta = 0;
2762 int ret = 0;
2764 ret = genpd_lock_interruptible(genpd);
2765 if (ret)
2766 return -ERESTARTSYS;
2768 if (genpd->status == GPD_STATE_ACTIVE)
2769 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2771 seq_printf(s, "%lld ms\n", ktime_to_ms(
2772 ktime_add(genpd->on_time, delta)));
2774 genpd_unlock(genpd);
2775 return ret;
2778 static int genpd_total_idle_time_show(struct seq_file *s, void *data)
2780 struct generic_pm_domain *genpd = s->private;
2781 ktime_t delta = 0, total = 0;
2782 unsigned int i;
2783 int ret = 0;
2785 ret = genpd_lock_interruptible(genpd);
2786 if (ret)
2787 return -ERESTARTSYS;
2789 for (i = 0; i < genpd->state_count; i++) {
2791 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2792 (genpd->state_idx == i))
2793 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2795 total = ktime_add(total, genpd->states[i].idle_time);
2797 total = ktime_add(total, delta);
2799 seq_printf(s, "%lld ms\n", ktime_to_ms(total));
2801 genpd_unlock(genpd);
2802 return ret;
2806 static int genpd_devices_show(struct seq_file *s, void *data)
2808 struct generic_pm_domain *genpd = s->private;
2809 struct pm_domain_data *pm_data;
2810 const char *kobj_path;
2811 int ret = 0;
2813 ret = genpd_lock_interruptible(genpd);
2814 if (ret)
2815 return -ERESTARTSYS;
2817 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2818 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2819 genpd_is_irq_safe(genpd) ?
2820 GFP_ATOMIC : GFP_KERNEL);
2821 if (kobj_path == NULL)
2822 continue;
2824 seq_printf(s, "%s\n", kobj_path);
2825 kfree(kobj_path);
2828 genpd_unlock(genpd);
2829 return ret;
2832 static int genpd_perf_state_show(struct seq_file *s, void *data)
2834 struct generic_pm_domain *genpd = s->private;
2836 if (genpd_lock_interruptible(genpd))
2837 return -ERESTARTSYS;
2839 seq_printf(s, "%u\n", genpd->performance_state);
2841 genpd_unlock(genpd);
2842 return 0;
2845 #define define_genpd_open_function(name) \
2846 static int genpd_##name##_open(struct inode *inode, struct file *file) \
2848 return single_open(file, genpd_##name##_show, inode->i_private); \
2851 define_genpd_open_function(summary);
2852 define_genpd_open_function(status);
2853 define_genpd_open_function(sub_domains);
2854 define_genpd_open_function(idle_states);
2855 define_genpd_open_function(active_time);
2856 define_genpd_open_function(total_idle_time);
2857 define_genpd_open_function(devices);
2858 define_genpd_open_function(perf_state);
2860 #define define_genpd_debugfs_fops(name) \
2861 static const struct file_operations genpd_##name##_fops = { \
2862 .open = genpd_##name##_open, \
2863 .read = seq_read, \
2864 .llseek = seq_lseek, \
2865 .release = single_release, \
2868 define_genpd_debugfs_fops(summary);
2869 define_genpd_debugfs_fops(status);
2870 define_genpd_debugfs_fops(sub_domains);
2871 define_genpd_debugfs_fops(idle_states);
2872 define_genpd_debugfs_fops(active_time);
2873 define_genpd_debugfs_fops(total_idle_time);
2874 define_genpd_debugfs_fops(devices);
2875 define_genpd_debugfs_fops(perf_state);
2877 static int __init genpd_debug_init(void)
2879 struct dentry *d;
2880 struct generic_pm_domain *genpd;
2882 genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2884 if (!genpd_debugfs_dir)
2885 return -ENOMEM;
2887 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
2888 genpd_debugfs_dir, NULL, &genpd_summary_fops);
2889 if (!d)
2890 return -ENOMEM;
2892 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2893 d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
2894 if (!d)
2895 return -ENOMEM;
2897 debugfs_create_file("current_state", 0444,
2898 d, genpd, &genpd_status_fops);
2899 debugfs_create_file("sub_domains", 0444,
2900 d, genpd, &genpd_sub_domains_fops);
2901 debugfs_create_file("idle_states", 0444,
2902 d, genpd, &genpd_idle_states_fops);
2903 debugfs_create_file("active_time", 0444,
2904 d, genpd, &genpd_active_time_fops);
2905 debugfs_create_file("total_idle_time", 0444,
2906 d, genpd, &genpd_total_idle_time_fops);
2907 debugfs_create_file("devices", 0444,
2908 d, genpd, &genpd_devices_fops);
2909 if (genpd->set_performance_state)
2910 debugfs_create_file("perf_state", 0444,
2911 d, genpd, &genpd_perf_state_fops);
2914 return 0;
2916 late_initcall(genpd_debug_init);
2918 static void __exit genpd_debug_exit(void)
2920 debugfs_remove_recursive(genpd_debugfs_dir);
2922 __exitcall(genpd_debug_exit);
2923 #endif /* CONFIG_DEBUG_FS */