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/init.h>
10 #include <linux/kernel.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/pm_domain.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/sched.h>
17 #include <linux/suspend.h>
19 static LIST_HEAD(gpd_list);
20 static DEFINE_MUTEX(gpd_list_lock);
24 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
26 if (IS_ERR_OR_NULL(dev->pm_domain))
27 return ERR_PTR(-EINVAL);
29 return pd_to_genpd(dev->pm_domain);
32 static void genpd_sd_counter_dec(struct generic_pm_domain *genpd)
34 if (!WARN_ON(genpd->sd_count == 0))
38 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
42 mutex_lock(&genpd->lock);
44 * Wait for the domain to transition into either the active,
45 * or the power off state.
48 prepare_to_wait(&genpd->status_wait_queue, &wait,
49 TASK_UNINTERRUPTIBLE);
50 if (genpd->status == GPD_STATE_ACTIVE
51 || genpd->status == GPD_STATE_POWER_OFF)
53 mutex_unlock(&genpd->lock);
57 mutex_lock(&genpd->lock);
59 finish_wait(&genpd->status_wait_queue, &wait);
62 static void genpd_release_lock(struct generic_pm_domain *genpd)
64 mutex_unlock(&genpd->lock);
67 static void genpd_set_active(struct generic_pm_domain *genpd)
69 if (genpd->resume_count == 0)
70 genpd->status = GPD_STATE_ACTIVE;
74 * pm_genpd_poweron - Restore power to a given PM domain and its parents.
75 * @genpd: PM domain to power up.
77 * Restore power to @genpd and all of its parents so that it is possible to
78 * resume a device belonging to it.
80 int pm_genpd_poweron(struct generic_pm_domain *genpd)
82 struct generic_pm_domain *parent = genpd->parent;
88 genpd_acquire_lock(parent);
89 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
91 mutex_lock(&genpd->lock);
94 if (genpd->status == GPD_STATE_ACTIVE
95 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
98 if (genpd->status != GPD_STATE_POWER_OFF) {
99 genpd_set_active(genpd);
103 if (parent && parent->status != GPD_STATE_ACTIVE) {
104 mutex_unlock(&genpd->lock);
105 genpd_release_lock(parent);
107 ret = pm_genpd_poweron(parent);
114 if (genpd->power_on) {
115 int ret = genpd->power_on(genpd);
120 genpd_set_active(genpd);
125 mutex_unlock(&genpd->lock);
127 genpd_release_lock(parent);
132 #endif /* CONFIG_PM */
134 #ifdef CONFIG_PM_RUNTIME
137 * __pm_genpd_save_device - Save the pre-suspend state of a device.
138 * @dle: Device list entry of the device to save the state of.
139 * @genpd: PM domain the device belongs to.
141 static int __pm_genpd_save_device(struct dev_list_entry *dle,
142 struct generic_pm_domain *genpd)
143 __releases(&genpd->lock) __acquires(&genpd->lock)
145 struct device *dev = dle->dev;
146 struct device_driver *drv = dev->driver;
149 if (dle->need_restore)
152 mutex_unlock(&genpd->lock);
154 if (drv && drv->pm && drv->pm->runtime_suspend) {
155 if (genpd->start_device)
156 genpd->start_device(dev);
158 ret = drv->pm->runtime_suspend(dev);
160 if (genpd->stop_device)
161 genpd->stop_device(dev);
164 mutex_lock(&genpd->lock);
167 dle->need_restore = true;
173 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
174 * @dle: Device list entry of the device to restore the state of.
175 * @genpd: PM domain the device belongs to.
177 static void __pm_genpd_restore_device(struct dev_list_entry *dle,
178 struct generic_pm_domain *genpd)
179 __releases(&genpd->lock) __acquires(&genpd->lock)
181 struct device *dev = dle->dev;
182 struct device_driver *drv = dev->driver;
184 if (!dle->need_restore)
187 mutex_unlock(&genpd->lock);
189 if (drv && drv->pm && drv->pm->runtime_resume) {
190 if (genpd->start_device)
191 genpd->start_device(dev);
193 drv->pm->runtime_resume(dev);
195 if (genpd->stop_device)
196 genpd->stop_device(dev);
199 mutex_lock(&genpd->lock);
201 dle->need_restore = false;
205 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
206 * @genpd: PM domain to check.
208 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
209 * a "power off" operation, which means that a "power on" has occured in the
210 * meantime, or if its resume_count field is different from zero, which means
211 * that one of its devices has been resumed in the meantime.
213 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
215 return genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
219 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
220 * @genpd: PM domait to power off.
222 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
225 void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
227 if (!work_pending(&genpd->power_off_work))
228 queue_work(pm_wq, &genpd->power_off_work);
232 * pm_genpd_poweroff - Remove power from a given PM domain.
233 * @genpd: PM domain to power down.
235 * If all of the @genpd's devices have been suspended and all of its subdomains
236 * have been powered down, run the runtime suspend callbacks provided by all of
237 * the @genpd's devices' drivers and remove power from @genpd.
239 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
240 __releases(&genpd->lock) __acquires(&genpd->lock)
242 struct generic_pm_domain *parent;
243 struct dev_list_entry *dle;
244 unsigned int not_suspended;
249 * Do not try to power off the domain in the following situations:
250 * (1) The domain is already in the "power off" state.
251 * (2) System suspend is in progress.
252 * (3) One of the domain's devices is being resumed right now.
254 if (genpd->status == GPD_STATE_POWER_OFF || genpd->prepared_count > 0
255 || genpd->resume_count > 0)
258 if (genpd->sd_count > 0)
262 list_for_each_entry(dle, &genpd->dev_list, node)
263 if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
266 if (not_suspended > genpd->in_progress)
269 if (genpd->poweroff_task) {
271 * Another instance of pm_genpd_poweroff() is executing
272 * callbacks, so tell it to start over and return.
274 genpd->status = GPD_STATE_REPEAT;
278 if (genpd->gov && genpd->gov->power_down_ok) {
279 if (!genpd->gov->power_down_ok(&genpd->domain))
283 genpd->status = GPD_STATE_BUSY;
284 genpd->poweroff_task = current;
286 list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
287 ret = __pm_genpd_save_device(dle, genpd);
289 genpd_set_active(genpd);
293 if (genpd_abort_poweroff(genpd))
296 if (genpd->status == GPD_STATE_REPEAT) {
297 genpd->poweroff_task = NULL;
302 parent = genpd->parent;
304 mutex_unlock(&genpd->lock);
306 genpd_acquire_lock(parent);
307 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
309 if (genpd_abort_poweroff(genpd)) {
310 genpd_release_lock(parent);
315 if (genpd->power_off)
316 genpd->power_off(genpd);
318 genpd->status = GPD_STATE_POWER_OFF;
321 genpd_sd_counter_dec(parent);
322 if (parent->sd_count == 0)
323 genpd_queue_power_off_work(parent);
325 genpd_release_lock(parent);
329 genpd->poweroff_task = NULL;
330 wake_up_all(&genpd->status_wait_queue);
335 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
336 * @work: Work structure used for scheduling the execution of this function.
338 static void genpd_power_off_work_fn(struct work_struct *work)
340 struct generic_pm_domain *genpd;
342 genpd = container_of(work, struct generic_pm_domain, power_off_work);
344 genpd_acquire_lock(genpd);
345 pm_genpd_poweroff(genpd);
346 genpd_release_lock(genpd);
350 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
351 * @dev: Device to suspend.
353 * Carry out a runtime suspend of a device under the assumption that its
354 * pm_domain field points to the domain member of an object of type
355 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
357 static int pm_genpd_runtime_suspend(struct device *dev)
359 struct generic_pm_domain *genpd;
361 dev_dbg(dev, "%s()\n", __func__);
363 genpd = dev_to_genpd(dev);
367 if (genpd->stop_device) {
368 int ret = genpd->stop_device(dev);
373 mutex_lock(&genpd->lock);
374 genpd->in_progress++;
375 pm_genpd_poweroff(genpd);
376 genpd->in_progress--;
377 mutex_unlock(&genpd->lock);
383 * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
384 * @dev: Device to resume.
385 * @genpd: PM domain the device belongs to.
387 static void __pm_genpd_runtime_resume(struct device *dev,
388 struct generic_pm_domain *genpd)
390 struct dev_list_entry *dle;
392 list_for_each_entry(dle, &genpd->dev_list, node) {
393 if (dle->dev == dev) {
394 __pm_genpd_restore_device(dle, genpd);
401 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
402 * @dev: Device to resume.
404 * Carry out a runtime resume of a device under the assumption that its
405 * pm_domain field points to the domain member of an object of type
406 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
408 static int pm_genpd_runtime_resume(struct device *dev)
410 struct generic_pm_domain *genpd;
414 dev_dbg(dev, "%s()\n", __func__);
416 genpd = dev_to_genpd(dev);
420 ret = pm_genpd_poweron(genpd);
424 mutex_lock(&genpd->lock);
425 genpd->status = GPD_STATE_BUSY;
426 genpd->resume_count++;
428 prepare_to_wait(&genpd->status_wait_queue, &wait,
429 TASK_UNINTERRUPTIBLE);
431 * If current is the powering off task, we have been called
432 * reentrantly from one of the device callbacks, so we should
435 if (!genpd->poweroff_task || genpd->poweroff_task == current)
437 mutex_unlock(&genpd->lock);
441 mutex_lock(&genpd->lock);
443 finish_wait(&genpd->status_wait_queue, &wait);
444 __pm_genpd_runtime_resume(dev, genpd);
445 genpd->resume_count--;
446 genpd_set_active(genpd);
447 wake_up_all(&genpd->status_wait_queue);
448 mutex_unlock(&genpd->lock);
450 if (genpd->start_device)
451 genpd->start_device(dev);
458 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
459 static inline void __pm_genpd_runtime_resume(struct device *dev,
460 struct generic_pm_domain *genpd) {}
462 #define pm_genpd_runtime_suspend NULL
463 #define pm_genpd_runtime_resume NULL
465 #endif /* CONFIG_PM_RUNTIME */
467 #ifdef CONFIG_PM_SLEEP
470 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
471 * @genpd: PM domain to power off, if possible.
473 * Check if the given PM domain can be powered off (during system suspend or
474 * hibernation) and do that if so. Also, in that case propagate to its parent.
476 * This function is only called in "noirq" stages of system power transitions,
477 * so it need not acquire locks (all of the "noirq" callbacks are executed
478 * sequentially, so it is guaranteed that it will never run twice in parallel).
480 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
482 struct generic_pm_domain *parent = genpd->parent;
484 if (genpd->status == GPD_STATE_POWER_OFF)
487 if (genpd->suspended_count != genpd->device_count || genpd->sd_count > 0)
490 if (genpd->power_off)
491 genpd->power_off(genpd);
493 genpd->status = GPD_STATE_POWER_OFF;
495 genpd_sd_counter_dec(parent);
496 pm_genpd_sync_poweroff(parent);
501 * resume_needed - Check whether to resume a device before system suspend.
502 * @dev: Device to check.
503 * @genpd: PM domain the device belongs to.
505 * There are two cases in which a device that can wake up the system from sleep
506 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
507 * to wake up the system and it has to remain active for this purpose while the
508 * system is in the sleep state and (2) if the device is not enabled to wake up
509 * the system from sleep states and it generally doesn't generate wakeup signals
510 * by itself (those signals are generated on its behalf by other parts of the
511 * system). In the latter case it may be necessary to reconfigure the device's
512 * wakeup settings during system suspend, because it may have been set up to
513 * signal remote wakeup from the system's working state as needed by runtime PM.
514 * Return 'true' in either of the above cases.
516 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
520 if (!device_can_wakeup(dev))
523 active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
524 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
528 * pm_genpd_prepare - Start power transition of a device in a PM domain.
529 * @dev: Device to start the transition of.
531 * Start a power transition of a device (during a system-wide power transition)
532 * under the assumption that its pm_domain field points to the domain member of
533 * an object of type struct generic_pm_domain representing a PM domain
534 * consisting of I/O devices.
536 static int pm_genpd_prepare(struct device *dev)
538 struct generic_pm_domain *genpd;
541 dev_dbg(dev, "%s()\n", __func__);
543 genpd = dev_to_genpd(dev);
548 * If a wakeup request is pending for the device, it should be woken up
549 * at this point and a system wakeup event should be reported if it's
550 * set up to wake up the system from sleep states.
552 pm_runtime_get_noresume(dev);
553 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
554 pm_wakeup_event(dev, 0);
556 if (pm_wakeup_pending()) {
557 pm_runtime_put_sync(dev);
561 if (resume_needed(dev, genpd))
562 pm_runtime_resume(dev);
564 genpd_acquire_lock(genpd);
566 if (genpd->prepared_count++ == 0)
567 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
569 genpd_release_lock(genpd);
571 if (genpd->suspend_power_off) {
572 pm_runtime_put_noidle(dev);
577 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
578 * so pm_genpd_poweron() will return immediately, but if the device
579 * is suspended (e.g. it's been stopped by .stop_device()), we need
580 * to make it operational.
582 pm_runtime_resume(dev);
583 __pm_runtime_disable(dev, false);
585 ret = pm_generic_prepare(dev);
587 mutex_lock(&genpd->lock);
589 if (--genpd->prepared_count == 0)
590 genpd->suspend_power_off = false;
592 mutex_unlock(&genpd->lock);
593 pm_runtime_enable(dev);
596 pm_runtime_put_sync(dev);
601 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
602 * @dev: Device to suspend.
604 * Suspend a device under the assumption that its pm_domain field points to the
605 * domain member of an object of type struct generic_pm_domain representing
606 * a PM domain consisting of I/O devices.
608 static int pm_genpd_suspend(struct device *dev)
610 struct generic_pm_domain *genpd;
612 dev_dbg(dev, "%s()\n", __func__);
614 genpd = dev_to_genpd(dev);
618 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
622 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
623 * @dev: Device to suspend.
625 * Carry out a late suspend of a device under the assumption that its
626 * pm_domain field points to the domain member of an object of type
627 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
629 static int pm_genpd_suspend_noirq(struct device *dev)
631 struct generic_pm_domain *genpd;
634 dev_dbg(dev, "%s()\n", __func__);
636 genpd = dev_to_genpd(dev);
640 if (genpd->suspend_power_off)
643 ret = pm_generic_suspend_noirq(dev);
647 if (device_may_wakeup(dev)
648 && genpd->active_wakeup && genpd->active_wakeup(dev))
651 if (genpd->stop_device)
652 genpd->stop_device(dev);
655 * Since all of the "noirq" callbacks are executed sequentially, it is
656 * guaranteed that this function will never run twice in parallel for
657 * the same PM domain, so it is not necessary to use locking here.
659 genpd->suspended_count++;
660 pm_genpd_sync_poweroff(genpd);
666 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
667 * @dev: Device to resume.
669 * Carry out an early resume of a device under the assumption that its
670 * pm_domain field points to the domain member of an object of type
671 * struct generic_pm_domain representing a power domain consisting of I/O
674 static int pm_genpd_resume_noirq(struct device *dev)
676 struct generic_pm_domain *genpd;
678 dev_dbg(dev, "%s()\n", __func__);
680 genpd = dev_to_genpd(dev);
684 if (genpd->suspend_power_off)
688 * Since all of the "noirq" callbacks are executed sequentially, it is
689 * guaranteed that this function will never run twice in parallel for
690 * the same PM domain, so it is not necessary to use locking here.
692 pm_genpd_poweron(genpd);
693 genpd->suspended_count--;
694 if (genpd->start_device)
695 genpd->start_device(dev);
697 return pm_generic_resume_noirq(dev);
701 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
702 * @dev: Device to resume.
704 * Resume a device under the assumption that its pm_domain field points to the
705 * domain member of an object of type struct generic_pm_domain representing
706 * a power domain consisting of I/O devices.
708 static int pm_genpd_resume(struct device *dev)
710 struct generic_pm_domain *genpd;
712 dev_dbg(dev, "%s()\n", __func__);
714 genpd = dev_to_genpd(dev);
718 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
722 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
723 * @dev: Device to freeze.
725 * Freeze a device under the assumption that its pm_domain field points to the
726 * domain member of an object of type struct generic_pm_domain representing
727 * a power domain consisting of I/O devices.
729 static int pm_genpd_freeze(struct device *dev)
731 struct generic_pm_domain *genpd;
733 dev_dbg(dev, "%s()\n", __func__);
735 genpd = dev_to_genpd(dev);
739 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
743 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
744 * @dev: Device to freeze.
746 * Carry out a late freeze of a device under the assumption that its
747 * pm_domain field points to the domain member of an object of type
748 * struct generic_pm_domain representing a power domain consisting of I/O
751 static int pm_genpd_freeze_noirq(struct device *dev)
753 struct generic_pm_domain *genpd;
756 dev_dbg(dev, "%s()\n", __func__);
758 genpd = dev_to_genpd(dev);
762 if (genpd->suspend_power_off)
765 ret = pm_generic_freeze_noirq(dev);
769 if (genpd->stop_device)
770 genpd->stop_device(dev);
776 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
777 * @dev: Device to thaw.
779 * Carry out an early thaw of a device under the assumption that its
780 * pm_domain field points to the domain member of an object of type
781 * struct generic_pm_domain representing a power domain consisting of I/O
784 static int pm_genpd_thaw_noirq(struct device *dev)
786 struct generic_pm_domain *genpd;
788 dev_dbg(dev, "%s()\n", __func__);
790 genpd = dev_to_genpd(dev);
794 if (genpd->suspend_power_off)
797 if (genpd->start_device)
798 genpd->start_device(dev);
800 return pm_generic_thaw_noirq(dev);
804 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
805 * @dev: Device to thaw.
807 * Thaw a device under the assumption that its pm_domain field points to the
808 * domain member of an object of type struct generic_pm_domain representing
809 * a power domain consisting of I/O devices.
811 static int pm_genpd_thaw(struct device *dev)
813 struct generic_pm_domain *genpd;
815 dev_dbg(dev, "%s()\n", __func__);
817 genpd = dev_to_genpd(dev);
821 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
825 * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
826 * @dev: Device to suspend.
828 * Power off a device under the assumption that its pm_domain field points to
829 * the domain member of an object of type struct generic_pm_domain representing
830 * a PM domain consisting of I/O devices.
832 static int pm_genpd_dev_poweroff(struct device *dev)
834 struct generic_pm_domain *genpd;
836 dev_dbg(dev, "%s()\n", __func__);
838 genpd = dev_to_genpd(dev);
842 return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
846 * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
847 * @dev: Device to suspend.
849 * Carry out a late powering off of a device under the assumption that its
850 * pm_domain field points to the domain member of an object of type
851 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
853 static int pm_genpd_dev_poweroff_noirq(struct device *dev)
855 struct generic_pm_domain *genpd;
858 dev_dbg(dev, "%s()\n", __func__);
860 genpd = dev_to_genpd(dev);
864 if (genpd->suspend_power_off)
867 ret = pm_generic_poweroff_noirq(dev);
871 if (device_may_wakeup(dev)
872 && genpd->active_wakeup && genpd->active_wakeup(dev))
875 if (genpd->stop_device)
876 genpd->stop_device(dev);
879 * Since all of the "noirq" callbacks are executed sequentially, it is
880 * guaranteed that this function will never run twice in parallel for
881 * the same PM domain, so it is not necessary to use locking here.
883 genpd->suspended_count++;
884 pm_genpd_sync_poweroff(genpd);
890 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
891 * @dev: Device to resume.
893 * Carry out an early restore of a device under the assumption that its
894 * pm_domain field points to the domain member of an object of type
895 * struct generic_pm_domain representing a power domain consisting of I/O
898 static int pm_genpd_restore_noirq(struct device *dev)
900 struct generic_pm_domain *genpd;
902 dev_dbg(dev, "%s()\n", __func__);
904 genpd = dev_to_genpd(dev);
909 * Since all of the "noirq" callbacks are executed sequentially, it is
910 * guaranteed that this function will never run twice in parallel for
911 * the same PM domain, so it is not necessary to use locking here.
913 genpd->status = GPD_STATE_POWER_OFF;
914 if (genpd->suspend_power_off) {
916 * The boot kernel might put the domain into the power on state,
917 * so make sure it really is powered off.
919 if (genpd->power_off)
920 genpd->power_off(genpd);
924 pm_genpd_poweron(genpd);
925 genpd->suspended_count--;
926 if (genpd->start_device)
927 genpd->start_device(dev);
929 return pm_generic_restore_noirq(dev);
933 * pm_genpd_restore - Restore a device belonging to an I/O power domain.
934 * @dev: Device to resume.
936 * Restore a device under the assumption that its pm_domain field points to the
937 * domain member of an object of type struct generic_pm_domain representing
938 * a power domain consisting of I/O devices.
940 static int pm_genpd_restore(struct device *dev)
942 struct generic_pm_domain *genpd;
944 dev_dbg(dev, "%s()\n", __func__);
946 genpd = dev_to_genpd(dev);
950 return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
954 * pm_genpd_complete - Complete power transition of a device in a power domain.
955 * @dev: Device to complete the transition of.
957 * Complete a power transition of a device (during a system-wide power
958 * transition) under the assumption that its pm_domain field points to the
959 * domain member of an object of type struct generic_pm_domain representing
960 * a power domain consisting of I/O devices.
962 static void pm_genpd_complete(struct device *dev)
964 struct generic_pm_domain *genpd;
967 dev_dbg(dev, "%s()\n", __func__);
969 genpd = dev_to_genpd(dev);
973 mutex_lock(&genpd->lock);
975 run_complete = !genpd->suspend_power_off;
976 if (--genpd->prepared_count == 0)
977 genpd->suspend_power_off = false;
979 mutex_unlock(&genpd->lock);
982 pm_generic_complete(dev);
983 pm_runtime_set_active(dev);
984 pm_runtime_enable(dev);
985 pm_runtime_idle(dev);
991 #define pm_genpd_prepare NULL
992 #define pm_genpd_suspend NULL
993 #define pm_genpd_suspend_noirq NULL
994 #define pm_genpd_resume_noirq NULL
995 #define pm_genpd_resume NULL
996 #define pm_genpd_freeze NULL
997 #define pm_genpd_freeze_noirq NULL
998 #define pm_genpd_thaw_noirq NULL
999 #define pm_genpd_thaw NULL
1000 #define pm_genpd_dev_poweroff_noirq NULL
1001 #define pm_genpd_dev_poweroff NULL
1002 #define pm_genpd_restore_noirq NULL
1003 #define pm_genpd_restore NULL
1004 #define pm_genpd_complete NULL
1006 #endif /* CONFIG_PM_SLEEP */
1009 * pm_genpd_add_device - Add a device to an I/O PM domain.
1010 * @genpd: PM domain to add the device to.
1011 * @dev: Device to be added.
1013 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1015 struct dev_list_entry *dle;
1018 dev_dbg(dev, "%s()\n", __func__);
1020 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1023 genpd_acquire_lock(genpd);
1025 if (genpd->status == GPD_STATE_POWER_OFF) {
1030 if (genpd->prepared_count > 0) {
1035 list_for_each_entry(dle, &genpd->dev_list, node)
1036 if (dle->dev == dev) {
1041 dle = kzalloc(sizeof(*dle), GFP_KERNEL);
1048 dle->need_restore = false;
1049 list_add_tail(&dle->node, &genpd->dev_list);
1050 genpd->device_count++;
1052 spin_lock_irq(&dev->power.lock);
1053 dev->pm_domain = &genpd->domain;
1054 spin_unlock_irq(&dev->power.lock);
1057 genpd_release_lock(genpd);
1063 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1064 * @genpd: PM domain to remove the device from.
1065 * @dev: Device to be removed.
1067 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1070 struct dev_list_entry *dle;
1073 dev_dbg(dev, "%s()\n", __func__);
1075 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1078 genpd_acquire_lock(genpd);
1080 if (genpd->prepared_count > 0) {
1085 list_for_each_entry(dle, &genpd->dev_list, node) {
1086 if (dle->dev != dev)
1089 spin_lock_irq(&dev->power.lock);
1090 dev->pm_domain = NULL;
1091 spin_unlock_irq(&dev->power.lock);
1093 genpd->device_count--;
1094 list_del(&dle->node);
1102 genpd_release_lock(genpd);
1108 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1109 * @genpd: Master PM domain to add the subdomain to.
1110 * @new_subdomain: Subdomain to be added.
1112 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1113 struct generic_pm_domain *new_subdomain)
1115 struct generic_pm_domain *subdomain;
1118 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
1122 genpd_acquire_lock(genpd);
1123 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
1125 if (new_subdomain->status != GPD_STATE_POWER_OFF
1126 && new_subdomain->status != GPD_STATE_ACTIVE) {
1127 mutex_unlock(&new_subdomain->lock);
1128 genpd_release_lock(genpd);
1132 if (genpd->status == GPD_STATE_POWER_OFF
1133 && new_subdomain->status != GPD_STATE_POWER_OFF) {
1138 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1139 if (subdomain == new_subdomain) {
1145 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
1146 new_subdomain->parent = genpd;
1147 if (subdomain->status != GPD_STATE_POWER_OFF)
1151 mutex_unlock(&new_subdomain->lock);
1152 genpd_release_lock(genpd);
1158 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1159 * @genpd: Master PM domain to remove the subdomain from.
1160 * @target: Subdomain to be removed.
1162 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1163 struct generic_pm_domain *target)
1165 struct generic_pm_domain *subdomain;
1168 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
1172 genpd_acquire_lock(genpd);
1174 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1175 if (subdomain != target)
1178 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1180 if (subdomain->status != GPD_STATE_POWER_OFF
1181 && subdomain->status != GPD_STATE_ACTIVE) {
1182 mutex_unlock(&subdomain->lock);
1183 genpd_release_lock(genpd);
1187 list_del(&subdomain->sd_node);
1188 subdomain->parent = NULL;
1189 if (subdomain->status != GPD_STATE_POWER_OFF)
1190 genpd_sd_counter_dec(genpd);
1192 mutex_unlock(&subdomain->lock);
1198 genpd_release_lock(genpd);
1204 * pm_genpd_init - Initialize a generic I/O PM domain object.
1205 * @genpd: PM domain object to initialize.
1206 * @gov: PM domain governor to associate with the domain (may be NULL).
1207 * @is_off: Initial value of the domain's power_is_off field.
1209 void pm_genpd_init(struct generic_pm_domain *genpd,
1210 struct dev_power_governor *gov, bool is_off)
1212 if (IS_ERR_OR_NULL(genpd))
1215 INIT_LIST_HEAD(&genpd->sd_node);
1216 genpd->parent = NULL;
1217 INIT_LIST_HEAD(&genpd->dev_list);
1218 INIT_LIST_HEAD(&genpd->sd_list);
1219 mutex_init(&genpd->lock);
1221 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1222 genpd->in_progress = 0;
1223 genpd->sd_count = 0;
1224 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1225 init_waitqueue_head(&genpd->status_wait_queue);
1226 genpd->poweroff_task = NULL;
1227 genpd->resume_count = 0;
1228 genpd->device_count = 0;
1229 genpd->suspended_count = 0;
1230 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1231 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1232 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
1233 genpd->domain.ops.prepare = pm_genpd_prepare;
1234 genpd->domain.ops.suspend = pm_genpd_suspend;
1235 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1236 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1237 genpd->domain.ops.resume = pm_genpd_resume;
1238 genpd->domain.ops.freeze = pm_genpd_freeze;
1239 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1240 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1241 genpd->domain.ops.thaw = pm_genpd_thaw;
1242 genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1243 genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1244 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1245 genpd->domain.ops.restore = pm_genpd_restore;
1246 genpd->domain.ops.complete = pm_genpd_complete;
1247 mutex_lock(&gpd_list_lock);
1248 list_add(&genpd->gpd_list_node, &gpd_list);
1249 mutex_unlock(&gpd_list_lock);
1253 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
1255 void pm_genpd_poweroff_unused(void)
1257 struct generic_pm_domain *genpd;
1259 mutex_lock(&gpd_list_lock);
1261 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
1262 genpd_queue_power_off_work(genpd);
1264 mutex_unlock(&gpd_list_lock);