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>
21 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
23 if (IS_ERR_OR_NULL(dev->pm_domain))
24 return ERR_PTR(-EINVAL);
26 return pd_to_genpd(dev->pm_domain);
29 static void genpd_sd_counter_dec(struct generic_pm_domain *genpd)
31 if (!WARN_ON(genpd->sd_count == 0))
35 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
39 mutex_lock(&genpd->lock);
41 * Wait for the domain to transition into either the active,
42 * or the power off state.
45 prepare_to_wait(&genpd->status_wait_queue, &wait,
46 TASK_UNINTERRUPTIBLE);
47 if (genpd->status == GPD_STATE_ACTIVE
48 || genpd->status == GPD_STATE_POWER_OFF)
50 mutex_unlock(&genpd->lock);
54 mutex_lock(&genpd->lock);
56 finish_wait(&genpd->status_wait_queue, &wait);
59 static void genpd_release_lock(struct generic_pm_domain *genpd)
61 mutex_unlock(&genpd->lock);
64 static void genpd_set_active(struct generic_pm_domain *genpd)
66 if (genpd->resume_count == 0)
67 genpd->status = GPD_STATE_ACTIVE;
71 * pm_genpd_poweron - Restore power to a given PM domain and its parents.
72 * @genpd: PM domain to power up.
74 * Restore power to @genpd and all of its parents so that it is possible to
75 * resume a device belonging to it.
77 int pm_genpd_poweron(struct generic_pm_domain *genpd)
79 struct generic_pm_domain *parent = genpd->parent;
85 genpd_acquire_lock(parent);
86 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
88 mutex_lock(&genpd->lock);
91 if (genpd->status == GPD_STATE_ACTIVE
92 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
95 if (genpd->status != GPD_STATE_POWER_OFF) {
96 genpd_set_active(genpd);
100 if (parent && parent->status != GPD_STATE_ACTIVE) {
101 mutex_unlock(&genpd->lock);
102 genpd_release_lock(parent);
104 ret = pm_genpd_poweron(parent);
111 if (genpd->power_on) {
112 int ret = genpd->power_on(genpd);
117 genpd_set_active(genpd);
122 mutex_unlock(&genpd->lock);
124 genpd_release_lock(parent);
129 #endif /* CONFIG_PM */
131 #ifdef CONFIG_PM_RUNTIME
134 * __pm_genpd_save_device - Save the pre-suspend state of a device.
135 * @dle: Device list entry of the device to save the state of.
136 * @genpd: PM domain the device belongs to.
138 static int __pm_genpd_save_device(struct dev_list_entry *dle,
139 struct generic_pm_domain *genpd)
140 __releases(&genpd->lock) __acquires(&genpd->lock)
142 struct device *dev = dle->dev;
143 struct device_driver *drv = dev->driver;
146 if (dle->need_restore)
149 mutex_unlock(&genpd->lock);
151 if (drv && drv->pm && drv->pm->runtime_suspend) {
152 if (genpd->start_device)
153 genpd->start_device(dev);
155 ret = drv->pm->runtime_suspend(dev);
157 if (genpd->stop_device)
158 genpd->stop_device(dev);
161 mutex_lock(&genpd->lock);
164 dle->need_restore = true;
170 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
171 * @dle: Device list entry of the device to restore the state of.
172 * @genpd: PM domain the device belongs to.
174 static void __pm_genpd_restore_device(struct dev_list_entry *dle,
175 struct generic_pm_domain *genpd)
176 __releases(&genpd->lock) __acquires(&genpd->lock)
178 struct device *dev = dle->dev;
179 struct device_driver *drv = dev->driver;
181 if (!dle->need_restore)
184 mutex_unlock(&genpd->lock);
186 if (drv && drv->pm && drv->pm->runtime_resume) {
187 if (genpd->start_device)
188 genpd->start_device(dev);
190 drv->pm->runtime_resume(dev);
192 if (genpd->stop_device)
193 genpd->stop_device(dev);
196 mutex_lock(&genpd->lock);
198 dle->need_restore = false;
202 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
203 * @genpd: PM domain to check.
205 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
206 * a "power off" operation, which means that a "power on" has occured in the
207 * meantime, or if its resume_count field is different from zero, which means
208 * that one of its devices has been resumed in the meantime.
210 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
212 return genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
216 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
217 * @genpd: PM domait to power off.
219 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
222 static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
224 if (!work_pending(&genpd->power_off_work))
225 queue_work(pm_wq, &genpd->power_off_work);
229 * pm_genpd_poweroff - Remove power from a given PM domain.
230 * @genpd: PM domain to power down.
232 * If all of the @genpd's devices have been suspended and all of its subdomains
233 * have been powered down, run the runtime suspend callbacks provided by all of
234 * the @genpd's devices' drivers and remove power from @genpd.
236 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
237 __releases(&genpd->lock) __acquires(&genpd->lock)
239 struct generic_pm_domain *parent;
240 struct dev_list_entry *dle;
241 unsigned int not_suspended;
246 * Do not try to power off the domain in the following situations:
247 * (1) The domain is already in the "power off" state.
248 * (2) System suspend is in progress.
249 * (3) One of the domain's devices is being resumed right now.
251 if (genpd->status == GPD_STATE_POWER_OFF || genpd->prepared_count > 0
252 || genpd->resume_count > 0)
255 if (genpd->sd_count > 0)
259 list_for_each_entry(dle, &genpd->dev_list, node)
260 if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
263 if (not_suspended > genpd->in_progress)
266 if (genpd->poweroff_task) {
268 * Another instance of pm_genpd_poweroff() is executing
269 * callbacks, so tell it to start over and return.
271 genpd->status = GPD_STATE_REPEAT;
275 if (genpd->gov && genpd->gov->power_down_ok) {
276 if (!genpd->gov->power_down_ok(&genpd->domain))
280 genpd->status = GPD_STATE_BUSY;
281 genpd->poweroff_task = current;
283 list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
284 ret = __pm_genpd_save_device(dle, genpd);
286 genpd_set_active(genpd);
290 if (genpd_abort_poweroff(genpd))
293 if (genpd->status == GPD_STATE_REPEAT) {
294 genpd->poweroff_task = NULL;
299 parent = genpd->parent;
301 mutex_unlock(&genpd->lock);
303 genpd_acquire_lock(parent);
304 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
306 if (genpd_abort_poweroff(genpd)) {
307 genpd_release_lock(parent);
312 if (genpd->power_off)
313 genpd->power_off(genpd);
315 genpd->status = GPD_STATE_POWER_OFF;
318 genpd_sd_counter_dec(parent);
319 if (parent->sd_count == 0)
320 genpd_queue_power_off_work(parent);
322 genpd_release_lock(parent);
326 genpd->poweroff_task = NULL;
327 wake_up_all(&genpd->status_wait_queue);
332 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
333 * @work: Work structure used for scheduling the execution of this function.
335 static void genpd_power_off_work_fn(struct work_struct *work)
337 struct generic_pm_domain *genpd;
339 genpd = container_of(work, struct generic_pm_domain, power_off_work);
341 genpd_acquire_lock(genpd);
342 pm_genpd_poweroff(genpd);
343 genpd_release_lock(genpd);
347 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
348 * @dev: Device to suspend.
350 * Carry out a runtime suspend of a device under the assumption that its
351 * pm_domain field points to the domain member of an object of type
352 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
354 static int pm_genpd_runtime_suspend(struct device *dev)
356 struct generic_pm_domain *genpd;
358 dev_dbg(dev, "%s()\n", __func__);
360 genpd = dev_to_genpd(dev);
364 if (genpd->stop_device) {
365 int ret = genpd->stop_device(dev);
370 mutex_lock(&genpd->lock);
371 genpd->in_progress++;
372 pm_genpd_poweroff(genpd);
373 genpd->in_progress--;
374 mutex_unlock(&genpd->lock);
380 * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
381 * @dev: Device to resume.
382 * @genpd: PM domain the device belongs to.
384 static void __pm_genpd_runtime_resume(struct device *dev,
385 struct generic_pm_domain *genpd)
387 struct dev_list_entry *dle;
389 list_for_each_entry(dle, &genpd->dev_list, node) {
390 if (dle->dev == dev) {
391 __pm_genpd_restore_device(dle, genpd);
398 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
399 * @dev: Device to resume.
401 * Carry out a runtime resume of a device under the assumption that its
402 * pm_domain field points to the domain member of an object of type
403 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
405 static int pm_genpd_runtime_resume(struct device *dev)
407 struct generic_pm_domain *genpd;
411 dev_dbg(dev, "%s()\n", __func__);
413 genpd = dev_to_genpd(dev);
417 ret = pm_genpd_poweron(genpd);
421 mutex_lock(&genpd->lock);
422 genpd->status = GPD_STATE_BUSY;
423 genpd->resume_count++;
425 prepare_to_wait(&genpd->status_wait_queue, &wait,
426 TASK_UNINTERRUPTIBLE);
428 * If current is the powering off task, we have been called
429 * reentrantly from one of the device callbacks, so we should
432 if (!genpd->poweroff_task || genpd->poweroff_task == current)
434 mutex_unlock(&genpd->lock);
438 mutex_lock(&genpd->lock);
440 finish_wait(&genpd->status_wait_queue, &wait);
441 __pm_genpd_runtime_resume(dev, genpd);
442 genpd->resume_count--;
443 genpd_set_active(genpd);
444 wake_up_all(&genpd->status_wait_queue);
445 mutex_unlock(&genpd->lock);
447 if (genpd->start_device)
448 genpd->start_device(dev);
455 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
456 static inline void __pm_genpd_runtime_resume(struct device *dev,
457 struct generic_pm_domain *genpd) {}
459 #define pm_genpd_runtime_suspend NULL
460 #define pm_genpd_runtime_resume NULL
462 #endif /* CONFIG_PM_RUNTIME */
464 #ifdef CONFIG_PM_SLEEP
467 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
468 * @genpd: PM domain to power off, if possible.
470 * Check if the given PM domain can be powered off (during system suspend or
471 * hibernation) and do that if so. Also, in that case propagate to its parent.
473 * This function is only called in "noirq" stages of system power transitions,
474 * so it need not acquire locks (all of the "noirq" callbacks are executed
475 * sequentially, so it is guaranteed that it will never run twice in parallel).
477 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
479 struct generic_pm_domain *parent = genpd->parent;
481 if (genpd->status == GPD_STATE_POWER_OFF)
484 if (genpd->suspended_count != genpd->device_count || genpd->sd_count > 0)
487 if (genpd->power_off)
488 genpd->power_off(genpd);
490 genpd->status = GPD_STATE_POWER_OFF;
492 genpd_sd_counter_dec(parent);
493 pm_genpd_sync_poweroff(parent);
498 * resume_needed - Check whether to resume a device before system suspend.
499 * @dev: Device to check.
500 * @genpd: PM domain the device belongs to.
502 * There are two cases in which a device that can wake up the system from sleep
503 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
504 * to wake up the system and it has to remain active for this purpose while the
505 * system is in the sleep state and (2) if the device is not enabled to wake up
506 * the system from sleep states and it generally doesn't generate wakeup signals
507 * by itself (those signals are generated on its behalf by other parts of the
508 * system). In the latter case it may be necessary to reconfigure the device's
509 * wakeup settings during system suspend, because it may have been set up to
510 * signal remote wakeup from the system's working state as needed by runtime PM.
511 * Return 'true' in either of the above cases.
513 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
517 if (!device_can_wakeup(dev))
520 active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
521 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
525 * pm_genpd_prepare - Start power transition of a device in a PM domain.
526 * @dev: Device to start the transition of.
528 * Start a power transition of a device (during a system-wide power transition)
529 * under the assumption that its pm_domain field points to the domain member of
530 * an object of type struct generic_pm_domain representing a PM domain
531 * consisting of I/O devices.
533 static int pm_genpd_prepare(struct device *dev)
535 struct generic_pm_domain *genpd;
538 dev_dbg(dev, "%s()\n", __func__);
540 genpd = dev_to_genpd(dev);
545 * If a wakeup request is pending for the device, it should be woken up
546 * at this point and a system wakeup event should be reported if it's
547 * set up to wake up the system from sleep states.
549 pm_runtime_get_noresume(dev);
550 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
551 pm_wakeup_event(dev, 0);
553 if (pm_wakeup_pending()) {
554 pm_runtime_put_sync(dev);
558 if (resume_needed(dev, genpd))
559 pm_runtime_resume(dev);
561 genpd_acquire_lock(genpd);
563 if (genpd->prepared_count++ == 0)
564 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
566 genpd_release_lock(genpd);
568 if (genpd->suspend_power_off) {
569 pm_runtime_put_noidle(dev);
574 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
575 * so pm_genpd_poweron() will return immediately, but if the device
576 * is suspended (e.g. it's been stopped by .stop_device()), we need
577 * to make it operational.
579 pm_runtime_resume(dev);
580 __pm_runtime_disable(dev, false);
582 ret = pm_generic_prepare(dev);
584 mutex_lock(&genpd->lock);
586 if (--genpd->prepared_count == 0)
587 genpd->suspend_power_off = false;
589 mutex_unlock(&genpd->lock);
590 pm_runtime_enable(dev);
593 pm_runtime_put_sync(dev);
598 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
599 * @dev: Device to suspend.
601 * Suspend a device under the assumption that its pm_domain field points to the
602 * domain member of an object of type struct generic_pm_domain representing
603 * a PM domain consisting of I/O devices.
605 static int pm_genpd_suspend(struct device *dev)
607 struct generic_pm_domain *genpd;
609 dev_dbg(dev, "%s()\n", __func__);
611 genpd = dev_to_genpd(dev);
615 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
619 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
620 * @dev: Device to suspend.
622 * Carry out a late suspend of a device under the assumption that its
623 * pm_domain field points to the domain member of an object of type
624 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
626 static int pm_genpd_suspend_noirq(struct device *dev)
628 struct generic_pm_domain *genpd;
631 dev_dbg(dev, "%s()\n", __func__);
633 genpd = dev_to_genpd(dev);
637 if (genpd->suspend_power_off)
640 ret = pm_generic_suspend_noirq(dev);
644 if (device_may_wakeup(dev)
645 && genpd->active_wakeup && genpd->active_wakeup(dev))
648 if (genpd->stop_device)
649 genpd->stop_device(dev);
652 * Since all of the "noirq" callbacks are executed sequentially, it is
653 * guaranteed that this function will never run twice in parallel for
654 * the same PM domain, so it is not necessary to use locking here.
656 genpd->suspended_count++;
657 pm_genpd_sync_poweroff(genpd);
663 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
664 * @dev: Device to resume.
666 * Carry out an early resume of a device under the assumption that its
667 * pm_domain field points to the domain member of an object of type
668 * struct generic_pm_domain representing a power domain consisting of I/O
671 static int pm_genpd_resume_noirq(struct device *dev)
673 struct generic_pm_domain *genpd;
675 dev_dbg(dev, "%s()\n", __func__);
677 genpd = dev_to_genpd(dev);
681 if (genpd->suspend_power_off)
685 * Since all of the "noirq" callbacks are executed sequentially, it is
686 * guaranteed that this function will never run twice in parallel for
687 * the same PM domain, so it is not necessary to use locking here.
689 pm_genpd_poweron(genpd);
690 genpd->suspended_count--;
691 if (genpd->start_device)
692 genpd->start_device(dev);
694 return pm_generic_resume_noirq(dev);
698 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
699 * @dev: Device to resume.
701 * Resume a device under the assumption that its pm_domain field points to the
702 * domain member of an object of type struct generic_pm_domain representing
703 * a power domain consisting of I/O devices.
705 static int pm_genpd_resume(struct device *dev)
707 struct generic_pm_domain *genpd;
709 dev_dbg(dev, "%s()\n", __func__);
711 genpd = dev_to_genpd(dev);
715 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
719 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
720 * @dev: Device to freeze.
722 * Freeze a device under the assumption that its pm_domain field points to the
723 * domain member of an object of type struct generic_pm_domain representing
724 * a power domain consisting of I/O devices.
726 static int pm_genpd_freeze(struct device *dev)
728 struct generic_pm_domain *genpd;
730 dev_dbg(dev, "%s()\n", __func__);
732 genpd = dev_to_genpd(dev);
736 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
740 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
741 * @dev: Device to freeze.
743 * Carry out a late freeze of a device under the assumption that its
744 * pm_domain field points to the domain member of an object of type
745 * struct generic_pm_domain representing a power domain consisting of I/O
748 static int pm_genpd_freeze_noirq(struct device *dev)
750 struct generic_pm_domain *genpd;
753 dev_dbg(dev, "%s()\n", __func__);
755 genpd = dev_to_genpd(dev);
759 if (genpd->suspend_power_off)
762 ret = pm_generic_freeze_noirq(dev);
766 if (genpd->stop_device)
767 genpd->stop_device(dev);
773 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
774 * @dev: Device to thaw.
776 * Carry out an early thaw of a device under the assumption that its
777 * pm_domain field points to the domain member of an object of type
778 * struct generic_pm_domain representing a power domain consisting of I/O
781 static int pm_genpd_thaw_noirq(struct device *dev)
783 struct generic_pm_domain *genpd;
785 dev_dbg(dev, "%s()\n", __func__);
787 genpd = dev_to_genpd(dev);
791 if (genpd->suspend_power_off)
794 if (genpd->start_device)
795 genpd->start_device(dev);
797 return pm_generic_thaw_noirq(dev);
801 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
802 * @dev: Device to thaw.
804 * Thaw a device under the assumption that its pm_domain field points to the
805 * domain member of an object of type struct generic_pm_domain representing
806 * a power domain consisting of I/O devices.
808 static int pm_genpd_thaw(struct device *dev)
810 struct generic_pm_domain *genpd;
812 dev_dbg(dev, "%s()\n", __func__);
814 genpd = dev_to_genpd(dev);
818 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
822 * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
823 * @dev: Device to suspend.
825 * Power off a device under the assumption that its pm_domain field points to
826 * the domain member of an object of type struct generic_pm_domain representing
827 * a PM domain consisting of I/O devices.
829 static int pm_genpd_dev_poweroff(struct device *dev)
831 struct generic_pm_domain *genpd;
833 dev_dbg(dev, "%s()\n", __func__);
835 genpd = dev_to_genpd(dev);
839 return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
843 * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
844 * @dev: Device to suspend.
846 * Carry out a late powering off of a device under the assumption that its
847 * pm_domain field points to the domain member of an object of type
848 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
850 static int pm_genpd_dev_poweroff_noirq(struct device *dev)
852 struct generic_pm_domain *genpd;
855 dev_dbg(dev, "%s()\n", __func__);
857 genpd = dev_to_genpd(dev);
861 if (genpd->suspend_power_off)
864 ret = pm_generic_poweroff_noirq(dev);
868 if (device_may_wakeup(dev)
869 && genpd->active_wakeup && genpd->active_wakeup(dev))
872 if (genpd->stop_device)
873 genpd->stop_device(dev);
876 * Since all of the "noirq" callbacks are executed sequentially, it is
877 * guaranteed that this function will never run twice in parallel for
878 * the same PM domain, so it is not necessary to use locking here.
880 genpd->suspended_count++;
881 pm_genpd_sync_poweroff(genpd);
887 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
888 * @dev: Device to resume.
890 * Carry out an early restore of a device under the assumption that its
891 * pm_domain field points to the domain member of an object of type
892 * struct generic_pm_domain representing a power domain consisting of I/O
895 static int pm_genpd_restore_noirq(struct device *dev)
897 struct generic_pm_domain *genpd;
899 dev_dbg(dev, "%s()\n", __func__);
901 genpd = dev_to_genpd(dev);
906 * Since all of the "noirq" callbacks are executed sequentially, it is
907 * guaranteed that this function will never run twice in parallel for
908 * the same PM domain, so it is not necessary to use locking here.
910 genpd->status = GPD_STATE_POWER_OFF;
911 if (genpd->suspend_power_off) {
913 * The boot kernel might put the domain into the power on state,
914 * so make sure it really is powered off.
916 if (genpd->power_off)
917 genpd->power_off(genpd);
921 pm_genpd_poweron(genpd);
922 genpd->suspended_count--;
923 if (genpd->start_device)
924 genpd->start_device(dev);
926 return pm_generic_restore_noirq(dev);
930 * pm_genpd_restore - Restore a device belonging to an I/O power domain.
931 * @dev: Device to resume.
933 * Restore a device under the assumption that its pm_domain field points to the
934 * domain member of an object of type struct generic_pm_domain representing
935 * a power domain consisting of I/O devices.
937 static int pm_genpd_restore(struct device *dev)
939 struct generic_pm_domain *genpd;
941 dev_dbg(dev, "%s()\n", __func__);
943 genpd = dev_to_genpd(dev);
947 return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
951 * pm_genpd_complete - Complete power transition of a device in a power domain.
952 * @dev: Device to complete the transition of.
954 * Complete a power transition of a device (during a system-wide power
955 * transition) under the assumption that its pm_domain field points to the
956 * domain member of an object of type struct generic_pm_domain representing
957 * a power domain consisting of I/O devices.
959 static void pm_genpd_complete(struct device *dev)
961 struct generic_pm_domain *genpd;
964 dev_dbg(dev, "%s()\n", __func__);
966 genpd = dev_to_genpd(dev);
970 mutex_lock(&genpd->lock);
972 run_complete = !genpd->suspend_power_off;
973 if (--genpd->prepared_count == 0)
974 genpd->suspend_power_off = false;
976 mutex_unlock(&genpd->lock);
979 pm_generic_complete(dev);
980 pm_runtime_set_active(dev);
981 pm_runtime_enable(dev);
982 pm_runtime_idle(dev);
988 #define pm_genpd_prepare NULL
989 #define pm_genpd_suspend NULL
990 #define pm_genpd_suspend_noirq NULL
991 #define pm_genpd_resume_noirq NULL
992 #define pm_genpd_resume NULL
993 #define pm_genpd_freeze NULL
994 #define pm_genpd_freeze_noirq NULL
995 #define pm_genpd_thaw_noirq NULL
996 #define pm_genpd_thaw NULL
997 #define pm_genpd_dev_poweroff_noirq NULL
998 #define pm_genpd_dev_poweroff NULL
999 #define pm_genpd_restore_noirq NULL
1000 #define pm_genpd_restore NULL
1001 #define pm_genpd_complete NULL
1003 #endif /* CONFIG_PM_SLEEP */
1006 * pm_genpd_add_device - Add a device to an I/O PM domain.
1007 * @genpd: PM domain to add the device to.
1008 * @dev: Device to be added.
1010 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1012 struct dev_list_entry *dle;
1015 dev_dbg(dev, "%s()\n", __func__);
1017 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1020 genpd_acquire_lock(genpd);
1022 if (genpd->status == GPD_STATE_POWER_OFF) {
1027 if (genpd->prepared_count > 0) {
1032 list_for_each_entry(dle, &genpd->dev_list, node)
1033 if (dle->dev == dev) {
1038 dle = kzalloc(sizeof(*dle), GFP_KERNEL);
1045 dle->need_restore = false;
1046 list_add_tail(&dle->node, &genpd->dev_list);
1047 genpd->device_count++;
1049 spin_lock_irq(&dev->power.lock);
1050 dev->pm_domain = &genpd->domain;
1051 spin_unlock_irq(&dev->power.lock);
1054 genpd_release_lock(genpd);
1060 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1061 * @genpd: PM domain to remove the device from.
1062 * @dev: Device to be removed.
1064 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1067 struct dev_list_entry *dle;
1070 dev_dbg(dev, "%s()\n", __func__);
1072 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1075 genpd_acquire_lock(genpd);
1077 if (genpd->prepared_count > 0) {
1082 list_for_each_entry(dle, &genpd->dev_list, node) {
1083 if (dle->dev != dev)
1086 spin_lock_irq(&dev->power.lock);
1087 dev->pm_domain = NULL;
1088 spin_unlock_irq(&dev->power.lock);
1090 genpd->device_count--;
1091 list_del(&dle->node);
1099 genpd_release_lock(genpd);
1105 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1106 * @genpd: Master PM domain to add the subdomain to.
1107 * @new_subdomain: Subdomain to be added.
1109 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1110 struct generic_pm_domain *new_subdomain)
1112 struct generic_pm_domain *subdomain;
1115 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
1119 genpd_acquire_lock(genpd);
1120 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
1122 if (new_subdomain->status != GPD_STATE_POWER_OFF
1123 && new_subdomain->status != GPD_STATE_ACTIVE) {
1124 mutex_unlock(&new_subdomain->lock);
1125 genpd_release_lock(genpd);
1129 if (genpd->status == GPD_STATE_POWER_OFF
1130 && new_subdomain->status != GPD_STATE_POWER_OFF) {
1135 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1136 if (subdomain == new_subdomain) {
1142 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
1143 new_subdomain->parent = genpd;
1144 if (subdomain->status != GPD_STATE_POWER_OFF)
1148 mutex_unlock(&new_subdomain->lock);
1149 genpd_release_lock(genpd);
1155 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1156 * @genpd: Master PM domain to remove the subdomain from.
1157 * @target: Subdomain to be removed.
1159 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1160 struct generic_pm_domain *target)
1162 struct generic_pm_domain *subdomain;
1165 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
1169 genpd_acquire_lock(genpd);
1171 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1172 if (subdomain != target)
1175 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1177 if (subdomain->status != GPD_STATE_POWER_OFF
1178 && subdomain->status != GPD_STATE_ACTIVE) {
1179 mutex_unlock(&subdomain->lock);
1180 genpd_release_lock(genpd);
1184 list_del(&subdomain->sd_node);
1185 subdomain->parent = NULL;
1186 if (subdomain->status != GPD_STATE_POWER_OFF)
1187 genpd_sd_counter_dec(genpd);
1189 mutex_unlock(&subdomain->lock);
1195 genpd_release_lock(genpd);
1201 * pm_genpd_init - Initialize a generic I/O PM domain object.
1202 * @genpd: PM domain object to initialize.
1203 * @gov: PM domain governor to associate with the domain (may be NULL).
1204 * @is_off: Initial value of the domain's power_is_off field.
1206 void pm_genpd_init(struct generic_pm_domain *genpd,
1207 struct dev_power_governor *gov, bool is_off)
1209 if (IS_ERR_OR_NULL(genpd))
1212 INIT_LIST_HEAD(&genpd->sd_node);
1213 genpd->parent = NULL;
1214 INIT_LIST_HEAD(&genpd->dev_list);
1215 INIT_LIST_HEAD(&genpd->sd_list);
1216 mutex_init(&genpd->lock);
1218 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1219 genpd->in_progress = 0;
1220 genpd->sd_count = 0;
1221 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1222 init_waitqueue_head(&genpd->status_wait_queue);
1223 genpd->poweroff_task = NULL;
1224 genpd->resume_count = 0;
1225 genpd->device_count = 0;
1226 genpd->suspended_count = 0;
1227 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1228 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1229 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
1230 genpd->domain.ops.prepare = pm_genpd_prepare;
1231 genpd->domain.ops.suspend = pm_genpd_suspend;
1232 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1233 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1234 genpd->domain.ops.resume = pm_genpd_resume;
1235 genpd->domain.ops.freeze = pm_genpd_freeze;
1236 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1237 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1238 genpd->domain.ops.thaw = pm_genpd_thaw;
1239 genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1240 genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1241 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1242 genpd->domain.ops.restore = pm_genpd_restore;
1243 genpd->domain.ops.complete = pm_genpd_complete;