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.
9 #include <linux/delay.h>
10 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/pm_domain.h>
15 #include <linux/pm_qos.h>
16 #include <linux/pm_clock.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/sched.h>
20 #include <linux/suspend.h>
21 #include <linux/export.h>
23 #define GENPD_RETRY_MAX_MS 250 /* Approximate */
25 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
27 type (*__routine)(struct device *__d); \
28 type __ret = (type)0; \
30 __routine = genpd->dev_ops.callback; \
32 __ret = __routine(dev); \
37 static LIST_HEAD(gpd_list);
38 static DEFINE_MUTEX(gpd_list_lock);
41 * Get the generic PM domain for a particular struct device.
42 * This validates the struct device pointer, the PM domain pointer,
43 * and checks that the PM domain pointer is a real generic PM domain.
44 * Any failure results in NULL being returned.
46 struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
48 struct generic_pm_domain *genpd = NULL, *gpd;
50 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
53 mutex_lock(&gpd_list_lock);
54 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
55 if (&gpd->domain == dev->pm_domain) {
60 mutex_unlock(&gpd_list_lock);
66 * This should only be used where we are certain that the pm_domain
67 * attached to the device is a genpd domain.
69 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
71 if (IS_ERR_OR_NULL(dev->pm_domain))
72 return ERR_PTR(-EINVAL);
74 return pd_to_genpd(dev->pm_domain);
77 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
79 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
82 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
84 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
87 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
91 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
92 ret = !!atomic_dec_and_test(&genpd->sd_count);
97 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
99 atomic_inc(&genpd->sd_count);
100 smp_mb__after_atomic();
103 static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
109 if (!genpd->power_on)
113 return genpd->power_on(genpd);
115 time_start = ktime_get();
116 ret = genpd->power_on(genpd);
120 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
121 if (elapsed_ns <= genpd->power_on_latency_ns)
124 genpd->power_on_latency_ns = elapsed_ns;
125 genpd->max_off_time_changed = true;
126 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
127 genpd->name, "on", elapsed_ns);
132 static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
138 if (!genpd->power_off)
142 return genpd->power_off(genpd);
144 time_start = ktime_get();
145 ret = genpd->power_off(genpd);
149 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
150 if (elapsed_ns <= genpd->power_off_latency_ns)
153 genpd->power_off_latency_ns = elapsed_ns;
154 genpd->max_off_time_changed = true;
155 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
156 genpd->name, "off", elapsed_ns);
162 * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
163 * @genpd: PM domait to power off.
165 * Queue up the execution of genpd_poweroff() unless it's already been done
168 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
170 queue_work(pm_wq, &genpd->power_off_work);
174 * __genpd_poweron - Restore power to a given PM domain and its masters.
175 * @genpd: PM domain to power up.
176 * @depth: nesting count for lockdep.
178 * Restore power to @genpd and all of its masters so that it is possible to
179 * resume a device belonging to it.
181 static int __genpd_poweron(struct generic_pm_domain *genpd, unsigned int depth)
183 struct gpd_link *link;
186 if (genpd->status == GPD_STATE_ACTIVE)
190 * The list is guaranteed not to change while the loop below is being
191 * executed, unless one of the masters' .power_on() callbacks fiddles
194 list_for_each_entry(link, &genpd->slave_links, slave_node) {
195 struct generic_pm_domain *master = link->master;
197 genpd_sd_counter_inc(master);
199 mutex_lock_nested(&master->lock, depth + 1);
200 ret = __genpd_poweron(master, depth + 1);
201 mutex_unlock(&master->lock);
204 genpd_sd_counter_dec(master);
209 ret = genpd_power_on(genpd, true);
213 genpd->status = GPD_STATE_ACTIVE;
217 list_for_each_entry_continue_reverse(link,
220 genpd_sd_counter_dec(link->master);
221 genpd_queue_power_off_work(link->master);
228 * genpd_poweron - Restore power to a given PM domain and its masters.
229 * @genpd: PM domain to power up.
231 static int genpd_poweron(struct generic_pm_domain *genpd)
235 mutex_lock(&genpd->lock);
236 ret = __genpd_poweron(genpd, 0);
237 mutex_unlock(&genpd->lock);
242 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
244 return GENPD_DEV_CALLBACK(genpd, int, save_state, dev);
247 static int genpd_restore_dev(struct generic_pm_domain *genpd,
250 return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
253 static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
254 unsigned long val, void *ptr)
256 struct generic_pm_domain_data *gpd_data;
259 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
260 dev = gpd_data->base.dev;
263 struct generic_pm_domain *genpd;
264 struct pm_domain_data *pdd;
266 spin_lock_irq(&dev->power.lock);
268 pdd = dev->power.subsys_data ?
269 dev->power.subsys_data->domain_data : NULL;
270 if (pdd && pdd->dev) {
271 to_gpd_data(pdd)->td.constraint_changed = true;
272 genpd = dev_to_genpd(dev);
274 genpd = ERR_PTR(-ENODATA);
277 spin_unlock_irq(&dev->power.lock);
279 if (!IS_ERR(genpd)) {
280 mutex_lock(&genpd->lock);
281 genpd->max_off_time_changed = true;
282 mutex_unlock(&genpd->lock);
286 if (!dev || dev->power.ignore_children)
294 * genpd_poweroff - Remove power from a given PM domain.
295 * @genpd: PM domain to power down.
296 * @is_async: PM domain is powered down from a scheduled work
298 * If all of the @genpd's devices have been suspended and all of its subdomains
299 * have been powered down, remove power from @genpd.
301 static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
303 struct pm_domain_data *pdd;
304 struct gpd_link *link;
305 unsigned int not_suspended = 0;
308 * Do not try to power off the domain in the following situations:
309 * (1) The domain is already in the "power off" state.
310 * (2) System suspend is in progress.
312 if (genpd->status == GPD_STATE_POWER_OFF
313 || genpd->prepared_count > 0)
316 if (atomic_read(&genpd->sd_count) > 0)
319 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
320 enum pm_qos_flags_status stat;
322 stat = dev_pm_qos_flags(pdd->dev,
323 PM_QOS_FLAG_NO_POWER_OFF
324 | PM_QOS_FLAG_REMOTE_WAKEUP);
325 if (stat > PM_QOS_FLAGS_NONE)
328 if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
332 if (not_suspended > 1 || (not_suspended == 1 && is_async))
335 if (genpd->gov && genpd->gov->power_down_ok) {
336 if (!genpd->gov->power_down_ok(&genpd->domain))
340 if (genpd->power_off) {
343 if (atomic_read(&genpd->sd_count) > 0)
347 * If sd_count > 0 at this point, one of the subdomains hasn't
348 * managed to call genpd_poweron() for the master yet after
349 * incrementing it. In that case genpd_poweron() will wait
350 * for us to drop the lock, so we can call .power_off() and let
351 * the genpd_poweron() restore power for us (this shouldn't
352 * happen very often).
354 ret = genpd_power_off(genpd, true);
359 genpd->status = GPD_STATE_POWER_OFF;
361 list_for_each_entry(link, &genpd->slave_links, slave_node) {
362 genpd_sd_counter_dec(link->master);
363 genpd_queue_power_off_work(link->master);
370 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
371 * @work: Work structure used for scheduling the execution of this function.
373 static void genpd_power_off_work_fn(struct work_struct *work)
375 struct generic_pm_domain *genpd;
377 genpd = container_of(work, struct generic_pm_domain, power_off_work);
379 mutex_lock(&genpd->lock);
380 genpd_poweroff(genpd, true);
381 mutex_unlock(&genpd->lock);
385 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
386 * @dev: Device to suspend.
388 * Carry out a runtime suspend of a device under the assumption that its
389 * pm_domain field points to the domain member of an object of type
390 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
392 static int pm_genpd_runtime_suspend(struct device *dev)
394 struct generic_pm_domain *genpd;
395 bool (*stop_ok)(struct device *__dev);
396 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
397 bool runtime_pm = pm_runtime_enabled(dev);
402 dev_dbg(dev, "%s()\n", __func__);
404 genpd = dev_to_genpd(dev);
409 * A runtime PM centric subsystem/driver may re-use the runtime PM
410 * callbacks for other purposes than runtime PM. In those scenarios
411 * runtime PM is disabled. Under these circumstances, we shall skip
412 * validating/measuring the PM QoS latency.
414 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
415 if (runtime_pm && stop_ok && !stop_ok(dev))
418 /* Measure suspend latency. */
420 time_start = ktime_get();
422 ret = genpd_save_dev(genpd, dev);
426 ret = genpd_stop_dev(genpd, dev);
428 genpd_restore_dev(genpd, dev);
432 /* Update suspend latency value if the measured time exceeds it. */
434 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
435 if (elapsed_ns > td->suspend_latency_ns) {
436 td->suspend_latency_ns = elapsed_ns;
437 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
439 genpd->max_off_time_changed = true;
440 td->constraint_changed = true;
445 * If power.irq_safe is set, this routine will be run with interrupts
446 * off, so it can't use mutexes.
448 if (dev->power.irq_safe)
451 mutex_lock(&genpd->lock);
452 genpd_poweroff(genpd, false);
453 mutex_unlock(&genpd->lock);
459 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
460 * @dev: Device to resume.
462 * Carry out a runtime resume of a device under the assumption that its
463 * pm_domain field points to the domain member of an object of type
464 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
466 static int pm_genpd_runtime_resume(struct device *dev)
468 struct generic_pm_domain *genpd;
469 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
470 bool runtime_pm = pm_runtime_enabled(dev);
476 dev_dbg(dev, "%s()\n", __func__);
478 genpd = dev_to_genpd(dev);
482 /* If power.irq_safe, the PM domain is never powered off. */
483 if (dev->power.irq_safe) {
488 mutex_lock(&genpd->lock);
489 ret = __genpd_poweron(genpd, 0);
490 mutex_unlock(&genpd->lock);
496 /* Measure resume latency. */
497 if (timed && runtime_pm)
498 time_start = ktime_get();
500 genpd_start_dev(genpd, dev);
501 genpd_restore_dev(genpd, dev);
503 /* Update resume latency value if the measured time exceeds it. */
504 if (timed && runtime_pm) {
505 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
506 if (elapsed_ns > td->resume_latency_ns) {
507 td->resume_latency_ns = elapsed_ns;
508 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
510 genpd->max_off_time_changed = true;
511 td->constraint_changed = true;
518 static bool pd_ignore_unused;
519 static int __init pd_ignore_unused_setup(char *__unused)
521 pd_ignore_unused = true;
524 __setup("pd_ignore_unused", pd_ignore_unused_setup);
527 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
529 static int __init genpd_poweroff_unused(void)
531 struct generic_pm_domain *genpd;
533 if (pd_ignore_unused) {
534 pr_warn("genpd: Not disabling unused power domains\n");
538 mutex_lock(&gpd_list_lock);
540 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
541 genpd_queue_power_off_work(genpd);
543 mutex_unlock(&gpd_list_lock);
547 late_initcall(genpd_poweroff_unused);
549 #ifdef CONFIG_PM_SLEEP
552 * pm_genpd_present - Check if the given PM domain has been initialized.
553 * @genpd: PM domain to check.
555 static bool pm_genpd_present(const struct generic_pm_domain *genpd)
557 const struct generic_pm_domain *gpd;
559 if (IS_ERR_OR_NULL(genpd))
562 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
569 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
572 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
576 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
577 * @genpd: PM domain to power off, if possible.
578 * @timed: True if latency measurements are allowed.
580 * Check if the given PM domain can be powered off (during system suspend or
581 * hibernation) and do that if so. Also, in that case propagate to its masters.
583 * This function is only called in "noirq" and "syscore" stages of system power
584 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
585 * executed sequentially, so it is guaranteed that it will never run twice in
588 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
591 struct gpd_link *link;
593 if (genpd->status == GPD_STATE_POWER_OFF)
596 if (genpd->suspended_count != genpd->device_count
597 || atomic_read(&genpd->sd_count) > 0)
600 genpd_power_off(genpd, timed);
602 genpd->status = GPD_STATE_POWER_OFF;
604 list_for_each_entry(link, &genpd->slave_links, slave_node) {
605 genpd_sd_counter_dec(link->master);
606 pm_genpd_sync_poweroff(link->master, timed);
611 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
612 * @genpd: PM domain to power on.
613 * @timed: True if latency measurements are allowed.
615 * This function is only called in "noirq" and "syscore" stages of system power
616 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
617 * executed sequentially, so it is guaranteed that it will never run twice in
620 static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
623 struct gpd_link *link;
625 if (genpd->status == GPD_STATE_ACTIVE)
628 list_for_each_entry(link, &genpd->slave_links, slave_node) {
629 pm_genpd_sync_poweron(link->master, timed);
630 genpd_sd_counter_inc(link->master);
633 genpd_power_on(genpd, timed);
635 genpd->status = GPD_STATE_ACTIVE;
639 * resume_needed - Check whether to resume a device before system suspend.
640 * @dev: Device to check.
641 * @genpd: PM domain the device belongs to.
643 * There are two cases in which a device that can wake up the system from sleep
644 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
645 * to wake up the system and it has to remain active for this purpose while the
646 * system is in the sleep state and (2) if the device is not enabled to wake up
647 * the system from sleep states and it generally doesn't generate wakeup signals
648 * by itself (those signals are generated on its behalf by other parts of the
649 * system). In the latter case it may be necessary to reconfigure the device's
650 * wakeup settings during system suspend, because it may have been set up to
651 * signal remote wakeup from the system's working state as needed by runtime PM.
652 * Return 'true' in either of the above cases.
654 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
658 if (!device_can_wakeup(dev))
661 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
662 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
666 * pm_genpd_prepare - Start power transition of a device in a PM domain.
667 * @dev: Device to start the transition of.
669 * Start a power transition of a device (during a system-wide power transition)
670 * under the assumption that its pm_domain field points to the domain member of
671 * an object of type struct generic_pm_domain representing a PM domain
672 * consisting of I/O devices.
674 static int pm_genpd_prepare(struct device *dev)
676 struct generic_pm_domain *genpd;
679 dev_dbg(dev, "%s()\n", __func__);
681 genpd = dev_to_genpd(dev);
686 * If a wakeup request is pending for the device, it should be woken up
687 * at this point and a system wakeup event should be reported if it's
688 * set up to wake up the system from sleep states.
690 if (resume_needed(dev, genpd))
691 pm_runtime_resume(dev);
693 mutex_lock(&genpd->lock);
695 if (genpd->prepared_count++ == 0) {
696 genpd->suspended_count = 0;
697 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
700 mutex_unlock(&genpd->lock);
702 ret = pm_generic_prepare(dev);
704 mutex_lock(&genpd->lock);
706 genpd->prepared_count--;
708 mutex_unlock(&genpd->lock);
715 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
716 * @dev: Device to suspend.
718 * Stop the device and remove power from the domain if all devices in it have
721 static int pm_genpd_suspend_noirq(struct device *dev)
723 struct generic_pm_domain *genpd;
725 dev_dbg(dev, "%s()\n", __func__);
727 genpd = dev_to_genpd(dev);
731 if (genpd->suspend_power_off ||
732 (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
736 * Since all of the "noirq" callbacks are executed sequentially, it is
737 * guaranteed that this function will never run twice in parallel for
738 * the same PM domain, so it is not necessary to use locking here.
740 genpd->suspended_count++;
741 pm_genpd_sync_poweroff(genpd, true);
747 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
748 * @dev: Device to resume.
750 * Restore power to the device's PM domain, if necessary, and start the device.
752 static int pm_genpd_resume_noirq(struct device *dev)
754 struct generic_pm_domain *genpd;
756 dev_dbg(dev, "%s()\n", __func__);
758 genpd = dev_to_genpd(dev);
762 if (genpd->suspend_power_off ||
763 (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
767 * Since all of the "noirq" callbacks are executed sequentially, it is
768 * guaranteed that this function will never run twice in parallel for
769 * the same PM domain, so it is not necessary to use locking here.
771 pm_genpd_sync_poweron(genpd, true);
772 genpd->suspended_count--;
778 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
779 * @dev: Device to freeze.
781 * Carry out a late freeze of a device under the assumption that its
782 * pm_domain field points to the domain member of an object of type
783 * struct generic_pm_domain representing a power domain consisting of I/O
786 static int pm_genpd_freeze_noirq(struct device *dev)
788 struct generic_pm_domain *genpd;
790 dev_dbg(dev, "%s()\n", __func__);
792 genpd = dev_to_genpd(dev);
800 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
801 * @dev: Device to thaw.
803 * Start the device, unless power has been removed from the domain already
804 * before the system transition.
806 static int pm_genpd_thaw_noirq(struct device *dev)
808 struct generic_pm_domain *genpd;
810 dev_dbg(dev, "%s()\n", __func__);
812 genpd = dev_to_genpd(dev);
820 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
821 * @dev: Device to resume.
823 * Make sure the domain will be in the same power state as before the
824 * hibernation the system is resuming from and start the device if necessary.
826 static int pm_genpd_restore_noirq(struct device *dev)
828 struct generic_pm_domain *genpd;
830 dev_dbg(dev, "%s()\n", __func__);
832 genpd = dev_to_genpd(dev);
837 * Since all of the "noirq" callbacks are executed sequentially, it is
838 * guaranteed that this function will never run twice in parallel for
839 * the same PM domain, so it is not necessary to use locking here.
841 * At this point suspended_count == 0 means we are being run for the
842 * first time for the given domain in the present cycle.
844 if (genpd->suspended_count++ == 0)
846 * The boot kernel might put the domain into arbitrary state,
847 * so make it appear as powered off to pm_genpd_sync_poweron(),
848 * so that it tries to power it on in case it was really off.
850 genpd->status = GPD_STATE_POWER_OFF;
852 pm_genpd_sync_poweron(genpd, true);
858 * pm_genpd_complete - Complete power transition of a device in a power domain.
859 * @dev: Device to complete the transition of.
861 * Complete a power transition of a device (during a system-wide power
862 * transition) under the assumption that its pm_domain field points to the
863 * domain member of an object of type struct generic_pm_domain representing
864 * a power domain consisting of I/O devices.
866 static void pm_genpd_complete(struct device *dev)
868 struct generic_pm_domain *genpd;
870 dev_dbg(dev, "%s()\n", __func__);
872 genpd = dev_to_genpd(dev);
876 pm_generic_complete(dev);
878 mutex_lock(&genpd->lock);
880 genpd->prepared_count--;
881 if (!genpd->prepared_count)
882 genpd_queue_power_off_work(genpd);
884 mutex_unlock(&genpd->lock);
888 * genpd_syscore_switch - Switch power during system core suspend or resume.
889 * @dev: Device that normally is marked as "always on" to switch power for.
891 * This routine may only be called during the system core (syscore) suspend or
892 * resume phase for devices whose "always on" flags are set.
894 static void genpd_syscore_switch(struct device *dev, bool suspend)
896 struct generic_pm_domain *genpd;
898 genpd = dev_to_genpd(dev);
899 if (!pm_genpd_present(genpd))
903 genpd->suspended_count++;
904 pm_genpd_sync_poweroff(genpd, false);
906 pm_genpd_sync_poweron(genpd, false);
907 genpd->suspended_count--;
911 void pm_genpd_syscore_poweroff(struct device *dev)
913 genpd_syscore_switch(dev, true);
915 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
917 void pm_genpd_syscore_poweron(struct device *dev)
919 genpd_syscore_switch(dev, false);
921 EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
923 #else /* !CONFIG_PM_SLEEP */
925 #define pm_genpd_prepare NULL
926 #define pm_genpd_suspend_noirq NULL
927 #define pm_genpd_resume_noirq NULL
928 #define pm_genpd_freeze_noirq NULL
929 #define pm_genpd_thaw_noirq NULL
930 #define pm_genpd_restore_noirq NULL
931 #define pm_genpd_complete NULL
933 #endif /* CONFIG_PM_SLEEP */
935 static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
936 struct generic_pm_domain *genpd,
937 struct gpd_timing_data *td)
939 struct generic_pm_domain_data *gpd_data;
942 ret = dev_pm_get_subsys_data(dev);
946 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
955 gpd_data->base.dev = dev;
956 gpd_data->td.constraint_changed = true;
957 gpd_data->td.effective_constraint_ns = -1;
958 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
960 spin_lock_irq(&dev->power.lock);
962 if (dev->power.subsys_data->domain_data) {
967 dev->power.subsys_data->domain_data = &gpd_data->base;
968 dev->pm_domain = &genpd->domain;
970 spin_unlock_irq(&dev->power.lock);
975 spin_unlock_irq(&dev->power.lock);
978 dev_pm_put_subsys_data(dev);
982 static void genpd_free_dev_data(struct device *dev,
983 struct generic_pm_domain_data *gpd_data)
985 spin_lock_irq(&dev->power.lock);
987 dev->pm_domain = NULL;
988 dev->power.subsys_data->domain_data = NULL;
990 spin_unlock_irq(&dev->power.lock);
993 dev_pm_put_subsys_data(dev);
997 * __pm_genpd_add_device - Add a device to an I/O PM domain.
998 * @genpd: PM domain to add the device to.
999 * @dev: Device to be added.
1000 * @td: Set of PM QoS timing parameters to attach to the device.
1002 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1003 struct gpd_timing_data *td)
1005 struct generic_pm_domain_data *gpd_data;
1008 dev_dbg(dev, "%s()\n", __func__);
1010 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1013 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1014 if (IS_ERR(gpd_data))
1015 return PTR_ERR(gpd_data);
1017 mutex_lock(&genpd->lock);
1019 if (genpd->prepared_count > 0) {
1024 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1028 genpd->device_count++;
1029 genpd->max_off_time_changed = true;
1031 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1034 mutex_unlock(&genpd->lock);
1037 genpd_free_dev_data(dev, gpd_data);
1039 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1045 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1046 * @genpd: PM domain to remove the device from.
1047 * @dev: Device to be removed.
1049 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1052 struct generic_pm_domain_data *gpd_data;
1053 struct pm_domain_data *pdd;
1056 dev_dbg(dev, "%s()\n", __func__);
1058 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
1061 /* The above validation also means we have existing domain_data. */
1062 pdd = dev->power.subsys_data->domain_data;
1063 gpd_data = to_gpd_data(pdd);
1064 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1066 mutex_lock(&genpd->lock);
1068 if (genpd->prepared_count > 0) {
1073 genpd->device_count--;
1074 genpd->max_off_time_changed = true;
1076 if (genpd->detach_dev)
1077 genpd->detach_dev(genpd, dev);
1079 list_del_init(&pdd->list_node);
1081 mutex_unlock(&genpd->lock);
1083 genpd_free_dev_data(dev, gpd_data);
1088 mutex_unlock(&genpd->lock);
1089 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1095 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1096 * @genpd: Master PM domain to add the subdomain to.
1097 * @subdomain: Subdomain to be added.
1099 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1100 struct generic_pm_domain *subdomain)
1102 struct gpd_link *link, *itr;
1105 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1106 || genpd == subdomain)
1109 link = kzalloc(sizeof(*link), GFP_KERNEL);
1113 mutex_lock(&subdomain->lock);
1114 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1116 if (genpd->status == GPD_STATE_POWER_OFF
1117 && subdomain->status != GPD_STATE_POWER_OFF) {
1122 list_for_each_entry(itr, &genpd->master_links, master_node) {
1123 if (itr->slave == subdomain && itr->master == genpd) {
1129 link->master = genpd;
1130 list_add_tail(&link->master_node, &genpd->master_links);
1131 link->slave = subdomain;
1132 list_add_tail(&link->slave_node, &subdomain->slave_links);
1133 if (subdomain->status != GPD_STATE_POWER_OFF)
1134 genpd_sd_counter_inc(genpd);
1137 mutex_unlock(&genpd->lock);
1138 mutex_unlock(&subdomain->lock);
1143 EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1146 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1147 * @genpd: Master PM domain to remove the subdomain from.
1148 * @subdomain: Subdomain to be removed.
1150 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1151 struct generic_pm_domain *subdomain)
1153 struct gpd_link *link;
1156 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1159 mutex_lock(&subdomain->lock);
1160 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
1162 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1163 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1169 list_for_each_entry(link, &genpd->master_links, master_node) {
1170 if (link->slave != subdomain)
1173 list_del(&link->master_node);
1174 list_del(&link->slave_node);
1176 if (subdomain->status != GPD_STATE_POWER_OFF)
1177 genpd_sd_counter_dec(genpd);
1184 mutex_unlock(&genpd->lock);
1185 mutex_unlock(&subdomain->lock);
1189 EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1191 /* Default device callbacks for generic PM domains. */
1194 * pm_genpd_default_save_state - Default "save device state" for PM domains.
1195 * @dev: Device to handle.
1197 static int pm_genpd_default_save_state(struct device *dev)
1199 int (*cb)(struct device *__dev);
1201 if (dev->type && dev->type->pm)
1202 cb = dev->type->pm->runtime_suspend;
1203 else if (dev->class && dev->class->pm)
1204 cb = dev->class->pm->runtime_suspend;
1205 else if (dev->bus && dev->bus->pm)
1206 cb = dev->bus->pm->runtime_suspend;
1210 if (!cb && dev->driver && dev->driver->pm)
1211 cb = dev->driver->pm->runtime_suspend;
1213 return cb ? cb(dev) : 0;
1217 * pm_genpd_default_restore_state - Default PM domains "restore device state".
1218 * @dev: Device to handle.
1220 static int pm_genpd_default_restore_state(struct device *dev)
1222 int (*cb)(struct device *__dev);
1224 if (dev->type && dev->type->pm)
1225 cb = dev->type->pm->runtime_resume;
1226 else if (dev->class && dev->class->pm)
1227 cb = dev->class->pm->runtime_resume;
1228 else if (dev->bus && dev->bus->pm)
1229 cb = dev->bus->pm->runtime_resume;
1233 if (!cb && dev->driver && dev->driver->pm)
1234 cb = dev->driver->pm->runtime_resume;
1236 return cb ? cb(dev) : 0;
1240 * pm_genpd_init - Initialize a generic I/O PM domain object.
1241 * @genpd: PM domain object to initialize.
1242 * @gov: PM domain governor to associate with the domain (may be NULL).
1243 * @is_off: Initial value of the domain's power_is_off field.
1245 void pm_genpd_init(struct generic_pm_domain *genpd,
1246 struct dev_power_governor *gov, bool is_off)
1248 if (IS_ERR_OR_NULL(genpd))
1251 INIT_LIST_HEAD(&genpd->master_links);
1252 INIT_LIST_HEAD(&genpd->slave_links);
1253 INIT_LIST_HEAD(&genpd->dev_list);
1254 mutex_init(&genpd->lock);
1256 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1257 atomic_set(&genpd->sd_count, 0);
1258 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1259 genpd->device_count = 0;
1260 genpd->max_off_time_ns = -1;
1261 genpd->max_off_time_changed = true;
1262 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1263 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1264 genpd->domain.ops.prepare = pm_genpd_prepare;
1265 genpd->domain.ops.suspend = pm_generic_suspend;
1266 genpd->domain.ops.suspend_late = pm_generic_suspend_late;
1267 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1268 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1269 genpd->domain.ops.resume_early = pm_generic_resume_early;
1270 genpd->domain.ops.resume = pm_generic_resume;
1271 genpd->domain.ops.freeze = pm_generic_freeze;
1272 genpd->domain.ops.freeze_late = pm_generic_freeze_late;
1273 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1274 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1275 genpd->domain.ops.thaw_early = pm_generic_thaw_early;
1276 genpd->domain.ops.thaw = pm_generic_thaw;
1277 genpd->domain.ops.poweroff = pm_generic_poweroff;
1278 genpd->domain.ops.poweroff_late = pm_generic_poweroff_late;
1279 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1280 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1281 genpd->domain.ops.restore_early = pm_generic_restore_early;
1282 genpd->domain.ops.restore = pm_generic_restore;
1283 genpd->domain.ops.complete = pm_genpd_complete;
1284 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1285 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1287 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1288 genpd->dev_ops.stop = pm_clk_suspend;
1289 genpd->dev_ops.start = pm_clk_resume;
1292 mutex_lock(&gpd_list_lock);
1293 list_add(&genpd->gpd_list_node, &gpd_list);
1294 mutex_unlock(&gpd_list_lock);
1296 EXPORT_SYMBOL_GPL(pm_genpd_init);
1298 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1300 * Device Tree based PM domain providers.
1302 * The code below implements generic device tree based PM domain providers that
1303 * bind device tree nodes with generic PM domains registered in the system.
1305 * Any driver that registers generic PM domains and needs to support binding of
1306 * devices to these domains is supposed to register a PM domain provider, which
1307 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1309 * Two simple mapping functions have been provided for convenience:
1310 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1311 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1316 * struct of_genpd_provider - PM domain provider registration structure
1317 * @link: Entry in global list of PM domain providers
1318 * @node: Pointer to device tree node of PM domain provider
1319 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1321 * @data: context pointer to be passed into @xlate callback
1323 struct of_genpd_provider {
1324 struct list_head link;
1325 struct device_node *node;
1326 genpd_xlate_t xlate;
1330 /* List of registered PM domain providers. */
1331 static LIST_HEAD(of_genpd_providers);
1332 /* Mutex to protect the list above. */
1333 static DEFINE_MUTEX(of_genpd_mutex);
1336 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1337 * @genpdspec: OF phandle args to map into a PM domain
1338 * @data: xlate function private data - pointer to struct generic_pm_domain
1340 * This is a generic xlate function that can be used to model PM domains that
1341 * have their own device tree nodes. The private data of xlate function needs
1342 * to be a valid pointer to struct generic_pm_domain.
1344 struct generic_pm_domain *__of_genpd_xlate_simple(
1345 struct of_phandle_args *genpdspec,
1348 if (genpdspec->args_count != 0)
1349 return ERR_PTR(-EINVAL);
1352 EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1355 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1356 * @genpdspec: OF phandle args to map into a PM domain
1357 * @data: xlate function private data - pointer to struct genpd_onecell_data
1359 * This is a generic xlate function that can be used to model simple PM domain
1360 * controllers that have one device tree node and provide multiple PM domains.
1361 * A single cell is used as an index into an array of PM domains specified in
1362 * the genpd_onecell_data struct when registering the provider.
1364 struct generic_pm_domain *__of_genpd_xlate_onecell(
1365 struct of_phandle_args *genpdspec,
1368 struct genpd_onecell_data *genpd_data = data;
1369 unsigned int idx = genpdspec->args[0];
1371 if (genpdspec->args_count != 1)
1372 return ERR_PTR(-EINVAL);
1374 if (idx >= genpd_data->num_domains) {
1375 pr_err("%s: invalid domain index %u\n", __func__, idx);
1376 return ERR_PTR(-EINVAL);
1379 if (!genpd_data->domains[idx])
1380 return ERR_PTR(-ENOENT);
1382 return genpd_data->domains[idx];
1384 EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1387 * __of_genpd_add_provider() - Register a PM domain provider for a node
1388 * @np: Device node pointer associated with the PM domain provider.
1389 * @xlate: Callback for decoding PM domain from phandle arguments.
1390 * @data: Context pointer for @xlate callback.
1392 int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1395 struct of_genpd_provider *cp;
1397 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1401 cp->node = of_node_get(np);
1405 mutex_lock(&of_genpd_mutex);
1406 list_add(&cp->link, &of_genpd_providers);
1407 mutex_unlock(&of_genpd_mutex);
1408 pr_debug("Added domain provider from %s\n", np->full_name);
1412 EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1415 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1416 * @np: Device node pointer associated with the PM domain provider
1418 void of_genpd_del_provider(struct device_node *np)
1420 struct of_genpd_provider *cp;
1422 mutex_lock(&of_genpd_mutex);
1423 list_for_each_entry(cp, &of_genpd_providers, link) {
1424 if (cp->node == np) {
1425 list_del(&cp->link);
1426 of_node_put(cp->node);
1431 mutex_unlock(&of_genpd_mutex);
1433 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1436 * of_genpd_get_from_provider() - Look-up PM domain
1437 * @genpdspec: OF phandle args to use for look-up
1439 * Looks for a PM domain provider under the node specified by @genpdspec and if
1440 * found, uses xlate function of the provider to map phandle args to a PM
1443 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1446 struct generic_pm_domain *of_genpd_get_from_provider(
1447 struct of_phandle_args *genpdspec)
1449 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1450 struct of_genpd_provider *provider;
1452 mutex_lock(&of_genpd_mutex);
1454 /* Check if we have such a provider in our array */
1455 list_for_each_entry(provider, &of_genpd_providers, link) {
1456 if (provider->node == genpdspec->np)
1457 genpd = provider->xlate(genpdspec, provider->data);
1462 mutex_unlock(&of_genpd_mutex);
1466 EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
1469 * genpd_dev_pm_detach - Detach a device from its PM domain.
1470 * @dev: Device to detach.
1471 * @power_off: Currently not used
1473 * Try to locate a corresponding generic PM domain, which the device was
1474 * attached to previously. If such is found, the device is detached from it.
1476 static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1478 struct generic_pm_domain *pd;
1482 pd = pm_genpd_lookup_dev(dev);
1486 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1488 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1489 ret = pm_genpd_remove_device(pd, dev);
1498 dev_err(dev, "failed to remove from PM domain %s: %d",
1503 /* Check if PM domain can be powered off after removing this device. */
1504 genpd_queue_power_off_work(pd);
1507 static void genpd_dev_pm_sync(struct device *dev)
1509 struct generic_pm_domain *pd;
1511 pd = dev_to_genpd(dev);
1515 genpd_queue_power_off_work(pd);
1519 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1520 * @dev: Device to attach.
1522 * Parse device's OF node to find a PM domain specifier. If such is found,
1523 * attaches the device to retrieved pm_domain ops.
1525 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1526 * backwards compatibility with existing DTBs.
1528 * Returns 0 on successfully attached PM domain or negative error code. Note
1529 * that if a power-domain exists for the device, but it cannot be found or
1530 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1531 * probed and to re-try again later.
1533 int genpd_dev_pm_attach(struct device *dev)
1535 struct of_phandle_args pd_args;
1536 struct generic_pm_domain *pd;
1546 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1547 "#power-domain-cells", 0, &pd_args);
1553 * Try legacy Samsung-specific bindings
1554 * (for backwards compatibility of DT ABI)
1556 pd_args.args_count = 0;
1557 pd_args.np = of_parse_phandle(dev->of_node,
1558 "samsung,power-domain", 0);
1563 pd = of_genpd_get_from_provider(&pd_args);
1564 of_node_put(pd_args.np);
1566 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1567 __func__, PTR_ERR(pd));
1568 return -EPROBE_DEFER;
1571 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1573 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1574 ret = pm_genpd_add_device(pd, dev);
1583 dev_err(dev, "failed to add to PM domain %s: %d",
1588 dev->pm_domain->detach = genpd_dev_pm_detach;
1589 dev->pm_domain->sync = genpd_dev_pm_sync;
1590 ret = genpd_poweron(pd);
1593 return ret ? -EPROBE_DEFER : 0;
1595 EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
1596 #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
1599 /*** debugfs support ***/
1601 #ifdef CONFIG_PM_ADVANCED_DEBUG
1602 #include <linux/pm.h>
1603 #include <linux/device.h>
1604 #include <linux/debugfs.h>
1605 #include <linux/seq_file.h>
1606 #include <linux/init.h>
1607 #include <linux/kobject.h>
1608 static struct dentry *pm_genpd_debugfs_dir;
1611 * TODO: This function is a slightly modified version of rtpm_status_show
1612 * from sysfs.c, so generalize it.
1614 static void rtpm_status_str(struct seq_file *s, struct device *dev)
1616 static const char * const status_lookup[] = {
1617 [RPM_ACTIVE] = "active",
1618 [RPM_RESUMING] = "resuming",
1619 [RPM_SUSPENDED] = "suspended",
1620 [RPM_SUSPENDING] = "suspending"
1624 if (dev->power.runtime_error)
1626 else if (dev->power.disable_depth)
1628 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1629 p = status_lookup[dev->power.runtime_status];
1636 static int pm_genpd_summary_one(struct seq_file *s,
1637 struct generic_pm_domain *genpd)
1639 static const char * const status_lookup[] = {
1640 [GPD_STATE_ACTIVE] = "on",
1641 [GPD_STATE_POWER_OFF] = "off"
1643 struct pm_domain_data *pm_data;
1644 const char *kobj_path;
1645 struct gpd_link *link;
1648 ret = mutex_lock_interruptible(&genpd->lock);
1650 return -ERESTARTSYS;
1652 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
1654 seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
1657 * Modifications on the list require holding locks on both
1658 * master and slave, so we are safe.
1659 * Also genpd->name is immutable.
1661 list_for_each_entry(link, &genpd->master_links, master_node) {
1662 seq_printf(s, "%s", link->slave->name);
1663 if (!list_is_last(&link->master_node, &genpd->master_links))
1667 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
1668 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1669 if (kobj_path == NULL)
1672 seq_printf(s, "\n %-50s ", kobj_path);
1673 rtpm_status_str(s, pm_data->dev);
1679 mutex_unlock(&genpd->lock);
1684 static int pm_genpd_summary_show(struct seq_file *s, void *data)
1686 struct generic_pm_domain *genpd;
1689 seq_puts(s, "domain status slaves\n");
1690 seq_puts(s, " /device runtime status\n");
1691 seq_puts(s, "----------------------------------------------------------------------\n");
1693 ret = mutex_lock_interruptible(&gpd_list_lock);
1695 return -ERESTARTSYS;
1697 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1698 ret = pm_genpd_summary_one(s, genpd);
1702 mutex_unlock(&gpd_list_lock);
1707 static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1709 return single_open(file, pm_genpd_summary_show, NULL);
1712 static const struct file_operations pm_genpd_summary_fops = {
1713 .open = pm_genpd_summary_open,
1715 .llseek = seq_lseek,
1716 .release = single_release,
1719 static int __init pm_genpd_debug_init(void)
1723 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1725 if (!pm_genpd_debugfs_dir)
1728 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1729 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1735 late_initcall(pm_genpd_debug_init);
1737 static void __exit pm_genpd_debug_exit(void)
1739 debugfs_remove_recursive(pm_genpd_debugfs_dir);
1741 __exitcall(pm_genpd_debug_exit);
1742 #endif /* CONFIG_PM_ADVANCED_DEBUG */