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 bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
36 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
37 ret = !!atomic_dec_and_test(&genpd->sd_count);
42 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
44 atomic_inc(&genpd->sd_count);
45 smp_mb__after_atomic_inc();
48 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
52 mutex_lock(&genpd->lock);
54 * Wait for the domain to transition into either the active,
55 * or the power off state.
58 prepare_to_wait(&genpd->status_wait_queue, &wait,
59 TASK_UNINTERRUPTIBLE);
60 if (genpd->status == GPD_STATE_ACTIVE
61 || genpd->status == GPD_STATE_POWER_OFF)
63 mutex_unlock(&genpd->lock);
67 mutex_lock(&genpd->lock);
69 finish_wait(&genpd->status_wait_queue, &wait);
72 static void genpd_release_lock(struct generic_pm_domain *genpd)
74 mutex_unlock(&genpd->lock);
77 static void genpd_set_active(struct generic_pm_domain *genpd)
79 if (genpd->resume_count == 0)
80 genpd->status = GPD_STATE_ACTIVE;
84 * pm_genpd_poweron - Restore power to a given PM domain and its parents.
85 * @genpd: PM domain to power up.
87 * Restore power to @genpd and all of its parents so that it is possible to
88 * resume a device belonging to it.
90 int pm_genpd_poweron(struct generic_pm_domain *genpd)
92 struct generic_pm_domain *parent = genpd->parent;
96 mutex_lock(&genpd->lock);
98 if (genpd->status == GPD_STATE_ACTIVE
99 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
102 if (genpd->status != GPD_STATE_POWER_OFF) {
103 genpd_set_active(genpd);
108 genpd_sd_counter_inc(parent);
110 mutex_unlock(&genpd->lock);
112 ret = pm_genpd_poweron(parent);
114 genpd_sd_counter_dec(parent);
123 ret = genpd->power_on(genpd);
127 genpd_sd_counter_dec(genpd->parent);
129 genpd_set_active(genpd);
133 mutex_unlock(&genpd->lock);
138 #endif /* CONFIG_PM */
140 #ifdef CONFIG_PM_RUNTIME
143 * __pm_genpd_save_device - Save the pre-suspend state of a device.
144 * @dle: Device list entry of the device to save the state of.
145 * @genpd: PM domain the device belongs to.
147 static int __pm_genpd_save_device(struct dev_list_entry *dle,
148 struct generic_pm_domain *genpd)
149 __releases(&genpd->lock) __acquires(&genpd->lock)
151 struct device *dev = dle->dev;
152 struct device_driver *drv = dev->driver;
155 if (dle->need_restore)
158 mutex_unlock(&genpd->lock);
160 if (drv && drv->pm && drv->pm->runtime_suspend) {
161 if (genpd->start_device)
162 genpd->start_device(dev);
164 ret = drv->pm->runtime_suspend(dev);
166 if (genpd->stop_device)
167 genpd->stop_device(dev);
170 mutex_lock(&genpd->lock);
173 dle->need_restore = true;
179 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
180 * @dle: Device list entry of the device to restore the state of.
181 * @genpd: PM domain the device belongs to.
183 static void __pm_genpd_restore_device(struct dev_list_entry *dle,
184 struct generic_pm_domain *genpd)
185 __releases(&genpd->lock) __acquires(&genpd->lock)
187 struct device *dev = dle->dev;
188 struct device_driver *drv = dev->driver;
190 if (!dle->need_restore)
193 mutex_unlock(&genpd->lock);
195 if (drv && drv->pm && drv->pm->runtime_resume) {
196 if (genpd->start_device)
197 genpd->start_device(dev);
199 drv->pm->runtime_resume(dev);
201 if (genpd->stop_device)
202 genpd->stop_device(dev);
205 mutex_lock(&genpd->lock);
207 dle->need_restore = false;
211 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
212 * @genpd: PM domain to check.
214 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
215 * a "power off" operation, which means that a "power on" has occured in the
216 * meantime, or if its resume_count field is different from zero, which means
217 * that one of its devices has been resumed in the meantime.
219 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
221 return genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
225 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
226 * @genpd: PM domait to power off.
228 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
231 void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
233 if (!work_pending(&genpd->power_off_work))
234 queue_work(pm_wq, &genpd->power_off_work);
238 * pm_genpd_poweroff - Remove power from a given PM domain.
239 * @genpd: PM domain to power down.
241 * If all of the @genpd's devices have been suspended and all of its subdomains
242 * have been powered down, run the runtime suspend callbacks provided by all of
243 * the @genpd's devices' drivers and remove power from @genpd.
245 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
246 __releases(&genpd->lock) __acquires(&genpd->lock)
248 struct generic_pm_domain *parent;
249 struct dev_list_entry *dle;
250 unsigned int not_suspended;
255 * Do not try to power off the domain in the following situations:
256 * (1) The domain is already in the "power off" state.
257 * (2) System suspend is in progress.
258 * (3) One of the domain's devices is being resumed right now.
260 if (genpd->status == GPD_STATE_POWER_OFF || genpd->prepared_count > 0
261 || genpd->resume_count > 0)
264 if (atomic_read(&genpd->sd_count) > 0)
268 list_for_each_entry(dle, &genpd->dev_list, node)
269 if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
272 if (not_suspended > genpd->in_progress)
275 if (genpd->poweroff_task) {
277 * Another instance of pm_genpd_poweroff() is executing
278 * callbacks, so tell it to start over and return.
280 genpd->status = GPD_STATE_REPEAT;
284 if (genpd->gov && genpd->gov->power_down_ok) {
285 if (!genpd->gov->power_down_ok(&genpd->domain))
289 genpd->status = GPD_STATE_BUSY;
290 genpd->poweroff_task = current;
292 list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
293 ret = atomic_read(&genpd->sd_count) == 0 ?
294 __pm_genpd_save_device(dle, genpd) : -EBUSY;
296 genpd_set_active(genpd);
300 if (genpd_abort_poweroff(genpd))
303 if (genpd->status == GPD_STATE_REPEAT) {
304 genpd->poweroff_task = NULL;
309 if (genpd->power_off) {
310 if (atomic_read(&genpd->sd_count) > 0) {
316 * If sd_count > 0 at this point, one of the children hasn't
317 * managed to call pm_genpd_poweron() for the parent yet after
318 * incrementing it. In that case pm_genpd_poweron() will wait
319 * for us to drop the lock, so we can call .power_off() and let
320 * the pm_genpd_poweron() restore power for us (this shouldn't
321 * happen very often).
323 ret = genpd->power_off(genpd);
325 genpd_set_active(genpd);
330 genpd->status = GPD_STATE_POWER_OFF;
332 parent = genpd->parent;
333 if (parent && genpd_sd_counter_dec(parent))
334 genpd_queue_power_off_work(parent);
337 genpd->poweroff_task = NULL;
338 wake_up_all(&genpd->status_wait_queue);
343 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
344 * @work: Work structure used for scheduling the execution of this function.
346 static void genpd_power_off_work_fn(struct work_struct *work)
348 struct generic_pm_domain *genpd;
350 genpd = container_of(work, struct generic_pm_domain, power_off_work);
352 genpd_acquire_lock(genpd);
353 pm_genpd_poweroff(genpd);
354 genpd_release_lock(genpd);
358 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
359 * @dev: Device to suspend.
361 * Carry out a runtime suspend of a device under the assumption that its
362 * pm_domain field points to the domain member of an object of type
363 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
365 static int pm_genpd_runtime_suspend(struct device *dev)
367 struct generic_pm_domain *genpd;
369 dev_dbg(dev, "%s()\n", __func__);
371 genpd = dev_to_genpd(dev);
375 if (genpd->stop_device) {
376 int ret = genpd->stop_device(dev);
381 mutex_lock(&genpd->lock);
382 genpd->in_progress++;
383 pm_genpd_poweroff(genpd);
384 genpd->in_progress--;
385 mutex_unlock(&genpd->lock);
391 * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
392 * @dev: Device to resume.
393 * @genpd: PM domain the device belongs to.
395 static void __pm_genpd_runtime_resume(struct device *dev,
396 struct generic_pm_domain *genpd)
398 struct dev_list_entry *dle;
400 list_for_each_entry(dle, &genpd->dev_list, node) {
401 if (dle->dev == dev) {
402 __pm_genpd_restore_device(dle, genpd);
409 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
410 * @dev: Device to resume.
412 * Carry out a runtime resume of a device under the assumption that its
413 * pm_domain field points to the domain member of an object of type
414 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
416 static int pm_genpd_runtime_resume(struct device *dev)
418 struct generic_pm_domain *genpd;
422 dev_dbg(dev, "%s()\n", __func__);
424 genpd = dev_to_genpd(dev);
428 ret = pm_genpd_poweron(genpd);
432 mutex_lock(&genpd->lock);
433 genpd->status = GPD_STATE_BUSY;
434 genpd->resume_count++;
436 prepare_to_wait(&genpd->status_wait_queue, &wait,
437 TASK_UNINTERRUPTIBLE);
439 * If current is the powering off task, we have been called
440 * reentrantly from one of the device callbacks, so we should
443 if (!genpd->poweroff_task || genpd->poweroff_task == current)
445 mutex_unlock(&genpd->lock);
449 mutex_lock(&genpd->lock);
451 finish_wait(&genpd->status_wait_queue, &wait);
452 __pm_genpd_runtime_resume(dev, genpd);
453 genpd->resume_count--;
454 genpd_set_active(genpd);
455 wake_up_all(&genpd->status_wait_queue);
456 mutex_unlock(&genpd->lock);
458 if (genpd->start_device)
459 genpd->start_device(dev);
465 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
467 void pm_genpd_poweroff_unused(void)
469 struct generic_pm_domain *genpd;
471 mutex_lock(&gpd_list_lock);
473 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
474 genpd_queue_power_off_work(genpd);
476 mutex_unlock(&gpd_list_lock);
481 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
482 static inline void __pm_genpd_runtime_resume(struct device *dev,
483 struct generic_pm_domain *genpd) {}
485 #define pm_genpd_runtime_suspend NULL
486 #define pm_genpd_runtime_resume NULL
488 #endif /* CONFIG_PM_RUNTIME */
490 #ifdef CONFIG_PM_SLEEP
493 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
494 * @genpd: PM domain to power off, if possible.
496 * Check if the given PM domain can be powered off (during system suspend or
497 * hibernation) and do that if so. Also, in that case propagate to its parent.
499 * This function is only called in "noirq" stages of system power transitions,
500 * so it need not acquire locks (all of the "noirq" callbacks are executed
501 * sequentially, so it is guaranteed that it will never run twice in parallel).
503 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
505 struct generic_pm_domain *parent = genpd->parent;
507 if (genpd->status == GPD_STATE_POWER_OFF)
510 if (genpd->suspended_count != genpd->device_count
511 || atomic_read(&genpd->sd_count) > 0)
514 if (genpd->power_off)
515 genpd->power_off(genpd);
517 genpd->status = GPD_STATE_POWER_OFF;
519 genpd_sd_counter_dec(parent);
520 pm_genpd_sync_poweroff(parent);
525 * resume_needed - Check whether to resume a device before system suspend.
526 * @dev: Device to check.
527 * @genpd: PM domain the device belongs to.
529 * There are two cases in which a device that can wake up the system from sleep
530 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
531 * to wake up the system and it has to remain active for this purpose while the
532 * system is in the sleep state and (2) if the device is not enabled to wake up
533 * the system from sleep states and it generally doesn't generate wakeup signals
534 * by itself (those signals are generated on its behalf by other parts of the
535 * system). In the latter case it may be necessary to reconfigure the device's
536 * wakeup settings during system suspend, because it may have been set up to
537 * signal remote wakeup from the system's working state as needed by runtime PM.
538 * Return 'true' in either of the above cases.
540 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
544 if (!device_can_wakeup(dev))
547 active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
548 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
552 * pm_genpd_prepare - Start power transition of a device in a PM domain.
553 * @dev: Device to start the transition of.
555 * Start a power transition of a device (during a system-wide power transition)
556 * under the assumption that its pm_domain field points to the domain member of
557 * an object of type struct generic_pm_domain representing a PM domain
558 * consisting of I/O devices.
560 static int pm_genpd_prepare(struct device *dev)
562 struct generic_pm_domain *genpd;
565 dev_dbg(dev, "%s()\n", __func__);
567 genpd = dev_to_genpd(dev);
572 * If a wakeup request is pending for the device, it should be woken up
573 * at this point and a system wakeup event should be reported if it's
574 * set up to wake up the system from sleep states.
576 pm_runtime_get_noresume(dev);
577 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
578 pm_wakeup_event(dev, 0);
580 if (pm_wakeup_pending()) {
581 pm_runtime_put_sync(dev);
585 if (resume_needed(dev, genpd))
586 pm_runtime_resume(dev);
588 genpd_acquire_lock(genpd);
590 if (genpd->prepared_count++ == 0)
591 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
593 genpd_release_lock(genpd);
595 if (genpd->suspend_power_off) {
596 pm_runtime_put_noidle(dev);
601 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
602 * so pm_genpd_poweron() will return immediately, but if the device
603 * is suspended (e.g. it's been stopped by .stop_device()), we need
604 * to make it operational.
606 pm_runtime_resume(dev);
607 __pm_runtime_disable(dev, false);
609 ret = pm_generic_prepare(dev);
611 mutex_lock(&genpd->lock);
613 if (--genpd->prepared_count == 0)
614 genpd->suspend_power_off = false;
616 mutex_unlock(&genpd->lock);
617 pm_runtime_enable(dev);
620 pm_runtime_put_sync(dev);
625 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
626 * @dev: Device to suspend.
628 * Suspend a device under the assumption that its pm_domain field points to the
629 * domain member of an object of type struct generic_pm_domain representing
630 * a PM domain consisting of I/O devices.
632 static int pm_genpd_suspend(struct device *dev)
634 struct generic_pm_domain *genpd;
636 dev_dbg(dev, "%s()\n", __func__);
638 genpd = dev_to_genpd(dev);
642 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
646 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
647 * @dev: Device to suspend.
649 * Carry out a late suspend of a device under the assumption that its
650 * pm_domain field points to the domain member of an object of type
651 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
653 static int pm_genpd_suspend_noirq(struct device *dev)
655 struct generic_pm_domain *genpd;
658 dev_dbg(dev, "%s()\n", __func__);
660 genpd = dev_to_genpd(dev);
664 if (genpd->suspend_power_off)
667 ret = pm_generic_suspend_noirq(dev);
671 if (device_may_wakeup(dev)
672 && genpd->active_wakeup && genpd->active_wakeup(dev))
675 if (genpd->stop_device)
676 genpd->stop_device(dev);
679 * Since all of the "noirq" callbacks are executed sequentially, it is
680 * guaranteed that this function will never run twice in parallel for
681 * the same PM domain, so it is not necessary to use locking here.
683 genpd->suspended_count++;
684 pm_genpd_sync_poweroff(genpd);
690 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
691 * @dev: Device to resume.
693 * Carry out an early resume of a device under the assumption that its
694 * pm_domain field points to the domain member of an object of type
695 * struct generic_pm_domain representing a power domain consisting of I/O
698 static int pm_genpd_resume_noirq(struct device *dev)
700 struct generic_pm_domain *genpd;
702 dev_dbg(dev, "%s()\n", __func__);
704 genpd = dev_to_genpd(dev);
708 if (genpd->suspend_power_off)
712 * Since all of the "noirq" callbacks are executed sequentially, it is
713 * guaranteed that this function will never run twice in parallel for
714 * the same PM domain, so it is not necessary to use locking here.
716 pm_genpd_poweron(genpd);
717 genpd->suspended_count--;
718 if (genpd->start_device)
719 genpd->start_device(dev);
721 return pm_generic_resume_noirq(dev);
725 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
726 * @dev: Device to resume.
728 * Resume a device under the assumption that its pm_domain field points to the
729 * domain member of an object of type struct generic_pm_domain representing
730 * a power domain consisting of I/O devices.
732 static int pm_genpd_resume(struct device *dev)
734 struct generic_pm_domain *genpd;
736 dev_dbg(dev, "%s()\n", __func__);
738 genpd = dev_to_genpd(dev);
742 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
746 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
747 * @dev: Device to freeze.
749 * Freeze a device under the assumption that its pm_domain field points to the
750 * domain member of an object of type struct generic_pm_domain representing
751 * a power domain consisting of I/O devices.
753 static int pm_genpd_freeze(struct device *dev)
755 struct generic_pm_domain *genpd;
757 dev_dbg(dev, "%s()\n", __func__);
759 genpd = dev_to_genpd(dev);
763 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
767 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
768 * @dev: Device to freeze.
770 * Carry out a late freeze of a device under the assumption that its
771 * pm_domain field points to the domain member of an object of type
772 * struct generic_pm_domain representing a power domain consisting of I/O
775 static int pm_genpd_freeze_noirq(struct device *dev)
777 struct generic_pm_domain *genpd;
780 dev_dbg(dev, "%s()\n", __func__);
782 genpd = dev_to_genpd(dev);
786 if (genpd->suspend_power_off)
789 ret = pm_generic_freeze_noirq(dev);
793 if (genpd->stop_device)
794 genpd->stop_device(dev);
800 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
801 * @dev: Device to thaw.
803 * Carry out an early thaw of a device under the assumption that its
804 * pm_domain field points to the domain member of an object of type
805 * struct generic_pm_domain representing a power domain consisting of I/O
808 static int pm_genpd_thaw_noirq(struct device *dev)
810 struct generic_pm_domain *genpd;
812 dev_dbg(dev, "%s()\n", __func__);
814 genpd = dev_to_genpd(dev);
818 if (genpd->suspend_power_off)
821 if (genpd->start_device)
822 genpd->start_device(dev);
824 return pm_generic_thaw_noirq(dev);
828 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
829 * @dev: Device to thaw.
831 * Thaw a device under the assumption that its pm_domain field points to the
832 * domain member of an object of type struct generic_pm_domain representing
833 * a power domain consisting of I/O devices.
835 static int pm_genpd_thaw(struct device *dev)
837 struct generic_pm_domain *genpd;
839 dev_dbg(dev, "%s()\n", __func__);
841 genpd = dev_to_genpd(dev);
845 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
849 * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
850 * @dev: Device to suspend.
852 * Power off a device under the assumption that its pm_domain field points to
853 * the domain member of an object of type struct generic_pm_domain representing
854 * a PM domain consisting of I/O devices.
856 static int pm_genpd_dev_poweroff(struct device *dev)
858 struct generic_pm_domain *genpd;
860 dev_dbg(dev, "%s()\n", __func__);
862 genpd = dev_to_genpd(dev);
866 return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
870 * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
871 * @dev: Device to suspend.
873 * Carry out a late powering off of a device under the assumption that its
874 * pm_domain field points to the domain member of an object of type
875 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
877 static int pm_genpd_dev_poweroff_noirq(struct device *dev)
879 struct generic_pm_domain *genpd;
882 dev_dbg(dev, "%s()\n", __func__);
884 genpd = dev_to_genpd(dev);
888 if (genpd->suspend_power_off)
891 ret = pm_generic_poweroff_noirq(dev);
895 if (device_may_wakeup(dev)
896 && genpd->active_wakeup && genpd->active_wakeup(dev))
899 if (genpd->stop_device)
900 genpd->stop_device(dev);
903 * Since all of the "noirq" callbacks are executed sequentially, it is
904 * guaranteed that this function will never run twice in parallel for
905 * the same PM domain, so it is not necessary to use locking here.
907 genpd->suspended_count++;
908 pm_genpd_sync_poweroff(genpd);
914 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
915 * @dev: Device to resume.
917 * Carry out an early restore of a device under the assumption that its
918 * pm_domain field points to the domain member of an object of type
919 * struct generic_pm_domain representing a power domain consisting of I/O
922 static int pm_genpd_restore_noirq(struct device *dev)
924 struct generic_pm_domain *genpd;
926 dev_dbg(dev, "%s()\n", __func__);
928 genpd = dev_to_genpd(dev);
933 * Since all of the "noirq" callbacks are executed sequentially, it is
934 * guaranteed that this function will never run twice in parallel for
935 * the same PM domain, so it is not necessary to use locking here.
937 genpd->status = GPD_STATE_POWER_OFF;
938 if (genpd->suspend_power_off) {
940 * The boot kernel might put the domain into the power on state,
941 * so make sure it really is powered off.
943 if (genpd->power_off)
944 genpd->power_off(genpd);
948 pm_genpd_poweron(genpd);
949 genpd->suspended_count--;
950 if (genpd->start_device)
951 genpd->start_device(dev);
953 return pm_generic_restore_noirq(dev);
957 * pm_genpd_restore - Restore a device belonging to an I/O power domain.
958 * @dev: Device to resume.
960 * Restore a device under the assumption that its pm_domain field points to the
961 * domain member of an object of type struct generic_pm_domain representing
962 * a power domain consisting of I/O devices.
964 static int pm_genpd_restore(struct device *dev)
966 struct generic_pm_domain *genpd;
968 dev_dbg(dev, "%s()\n", __func__);
970 genpd = dev_to_genpd(dev);
974 return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
978 * pm_genpd_complete - Complete power transition of a device in a power domain.
979 * @dev: Device to complete the transition of.
981 * Complete a power transition of a device (during a system-wide power
982 * transition) under the assumption that its pm_domain field points to the
983 * domain member of an object of type struct generic_pm_domain representing
984 * a power domain consisting of I/O devices.
986 static void pm_genpd_complete(struct device *dev)
988 struct generic_pm_domain *genpd;
991 dev_dbg(dev, "%s()\n", __func__);
993 genpd = dev_to_genpd(dev);
997 mutex_lock(&genpd->lock);
999 run_complete = !genpd->suspend_power_off;
1000 if (--genpd->prepared_count == 0)
1001 genpd->suspend_power_off = false;
1003 mutex_unlock(&genpd->lock);
1006 pm_generic_complete(dev);
1007 pm_runtime_set_active(dev);
1008 pm_runtime_enable(dev);
1009 pm_runtime_idle(dev);
1015 #define pm_genpd_prepare NULL
1016 #define pm_genpd_suspend NULL
1017 #define pm_genpd_suspend_noirq NULL
1018 #define pm_genpd_resume_noirq NULL
1019 #define pm_genpd_resume NULL
1020 #define pm_genpd_freeze NULL
1021 #define pm_genpd_freeze_noirq NULL
1022 #define pm_genpd_thaw_noirq NULL
1023 #define pm_genpd_thaw NULL
1024 #define pm_genpd_dev_poweroff_noirq NULL
1025 #define pm_genpd_dev_poweroff NULL
1026 #define pm_genpd_restore_noirq NULL
1027 #define pm_genpd_restore NULL
1028 #define pm_genpd_complete NULL
1030 #endif /* CONFIG_PM_SLEEP */
1033 * pm_genpd_add_device - Add a device to an I/O PM domain.
1034 * @genpd: PM domain to add the device to.
1035 * @dev: Device to be added.
1037 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1039 struct dev_list_entry *dle;
1042 dev_dbg(dev, "%s()\n", __func__);
1044 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1047 genpd_acquire_lock(genpd);
1049 if (genpd->status == GPD_STATE_POWER_OFF) {
1054 if (genpd->prepared_count > 0) {
1059 list_for_each_entry(dle, &genpd->dev_list, node)
1060 if (dle->dev == dev) {
1065 dle = kzalloc(sizeof(*dle), GFP_KERNEL);
1072 dle->need_restore = false;
1073 list_add_tail(&dle->node, &genpd->dev_list);
1074 genpd->device_count++;
1076 spin_lock_irq(&dev->power.lock);
1077 dev->pm_domain = &genpd->domain;
1078 spin_unlock_irq(&dev->power.lock);
1081 genpd_release_lock(genpd);
1087 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1088 * @genpd: PM domain to remove the device from.
1089 * @dev: Device to be removed.
1091 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1094 struct dev_list_entry *dle;
1097 dev_dbg(dev, "%s()\n", __func__);
1099 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1102 genpd_acquire_lock(genpd);
1104 if (genpd->prepared_count > 0) {
1109 list_for_each_entry(dle, &genpd->dev_list, node) {
1110 if (dle->dev != dev)
1113 spin_lock_irq(&dev->power.lock);
1114 dev->pm_domain = NULL;
1115 spin_unlock_irq(&dev->power.lock);
1117 genpd->device_count--;
1118 list_del(&dle->node);
1126 genpd_release_lock(genpd);
1132 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1133 * @genpd: Master PM domain to add the subdomain to.
1134 * @new_subdomain: Subdomain to be added.
1136 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1137 struct generic_pm_domain *new_subdomain)
1139 struct generic_pm_domain *subdomain;
1142 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
1146 genpd_acquire_lock(genpd);
1147 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
1149 if (new_subdomain->status != GPD_STATE_POWER_OFF
1150 && new_subdomain->status != GPD_STATE_ACTIVE) {
1151 mutex_unlock(&new_subdomain->lock);
1152 genpd_release_lock(genpd);
1156 if (genpd->status == GPD_STATE_POWER_OFF
1157 && new_subdomain->status != GPD_STATE_POWER_OFF) {
1162 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1163 if (subdomain == new_subdomain) {
1169 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
1170 new_subdomain->parent = genpd;
1171 if (subdomain->status != GPD_STATE_POWER_OFF)
1172 genpd_sd_counter_inc(genpd);
1175 mutex_unlock(&new_subdomain->lock);
1176 genpd_release_lock(genpd);
1182 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1183 * @genpd: Master PM domain to remove the subdomain from.
1184 * @target: Subdomain to be removed.
1186 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1187 struct generic_pm_domain *target)
1189 struct generic_pm_domain *subdomain;
1192 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
1196 genpd_acquire_lock(genpd);
1198 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1199 if (subdomain != target)
1202 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1204 if (subdomain->status != GPD_STATE_POWER_OFF
1205 && subdomain->status != GPD_STATE_ACTIVE) {
1206 mutex_unlock(&subdomain->lock);
1207 genpd_release_lock(genpd);
1211 list_del(&subdomain->sd_node);
1212 subdomain->parent = NULL;
1213 if (subdomain->status != GPD_STATE_POWER_OFF)
1214 genpd_sd_counter_dec(genpd);
1216 mutex_unlock(&subdomain->lock);
1222 genpd_release_lock(genpd);
1228 * pm_genpd_init - Initialize a generic I/O PM domain object.
1229 * @genpd: PM domain object to initialize.
1230 * @gov: PM domain governor to associate with the domain (may be NULL).
1231 * @is_off: Initial value of the domain's power_is_off field.
1233 void pm_genpd_init(struct generic_pm_domain *genpd,
1234 struct dev_power_governor *gov, bool is_off)
1236 if (IS_ERR_OR_NULL(genpd))
1239 INIT_LIST_HEAD(&genpd->sd_node);
1240 genpd->parent = NULL;
1241 INIT_LIST_HEAD(&genpd->dev_list);
1242 INIT_LIST_HEAD(&genpd->sd_list);
1243 mutex_init(&genpd->lock);
1245 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1246 genpd->in_progress = 0;
1247 atomic_set(&genpd->sd_count, 0);
1248 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1249 init_waitqueue_head(&genpd->status_wait_queue);
1250 genpd->poweroff_task = NULL;
1251 genpd->resume_count = 0;
1252 genpd->device_count = 0;
1253 genpd->suspended_count = 0;
1254 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1255 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1256 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
1257 genpd->domain.ops.prepare = pm_genpd_prepare;
1258 genpd->domain.ops.suspend = pm_genpd_suspend;
1259 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1260 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1261 genpd->domain.ops.resume = pm_genpd_resume;
1262 genpd->domain.ops.freeze = pm_genpd_freeze;
1263 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1264 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1265 genpd->domain.ops.thaw = pm_genpd_thaw;
1266 genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1267 genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1268 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1269 genpd->domain.ops.restore = pm_genpd_restore;
1270 genpd->domain.ops.complete = pm_genpd_complete;
1271 mutex_lock(&gpd_list_lock);
1272 list_add(&genpd->gpd_list_node, &gpd_list);
1273 mutex_unlock(&gpd_list_lock);