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)
91 __releases(&genpd->lock) __acquires(&genpd->lock)
96 /* If the domain's parent is being waited for, we have to wait too. */
98 prepare_to_wait(&genpd->status_wait_queue, &wait,
99 TASK_UNINTERRUPTIBLE);
100 if (genpd->status != GPD_STATE_WAIT_PARENT)
102 mutex_unlock(&genpd->lock);
106 mutex_lock(&genpd->lock);
108 finish_wait(&genpd->status_wait_queue, &wait);
110 if (genpd->status == GPD_STATE_ACTIVE
111 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
114 if (genpd->status != GPD_STATE_POWER_OFF) {
115 genpd_set_active(genpd);
120 genpd_sd_counter_inc(genpd->parent);
121 genpd->status = GPD_STATE_WAIT_PARENT;
123 mutex_unlock(&genpd->lock);
125 ret = pm_genpd_poweron(genpd->parent);
127 mutex_lock(&genpd->lock);
130 * The "wait for parent" status is guaranteed not to change
131 * while the parent is powering on.
133 genpd->status = GPD_STATE_POWER_OFF;
134 wake_up_all(&genpd->status_wait_queue);
139 if (genpd->power_on) {
140 ret = genpd->power_on(genpd);
145 genpd_set_active(genpd);
151 genpd_sd_counter_dec(genpd->parent);
157 * pm_genpd_poweron - Restore power to a given PM domain and its parents.
158 * @genpd: PM domain to power up.
160 int pm_genpd_poweron(struct generic_pm_domain *genpd)
164 mutex_lock(&genpd->lock);
165 ret = __pm_genpd_poweron(genpd);
166 mutex_unlock(&genpd->lock);
170 #endif /* CONFIG_PM */
172 #ifdef CONFIG_PM_RUNTIME
175 * __pm_genpd_save_device - Save the pre-suspend state of a device.
176 * @dle: Device list entry of the device to save the state of.
177 * @genpd: PM domain the device belongs to.
179 static int __pm_genpd_save_device(struct dev_list_entry *dle,
180 struct generic_pm_domain *genpd)
181 __releases(&genpd->lock) __acquires(&genpd->lock)
183 struct device *dev = dle->dev;
184 struct device_driver *drv = dev->driver;
187 if (dle->need_restore)
190 mutex_unlock(&genpd->lock);
192 if (drv && drv->pm && drv->pm->runtime_suspend) {
193 if (genpd->start_device)
194 genpd->start_device(dev);
196 ret = drv->pm->runtime_suspend(dev);
198 if (genpd->stop_device)
199 genpd->stop_device(dev);
202 mutex_lock(&genpd->lock);
205 dle->need_restore = true;
211 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
212 * @dle: Device list entry of the device to restore the state of.
213 * @genpd: PM domain the device belongs to.
215 static void __pm_genpd_restore_device(struct dev_list_entry *dle,
216 struct generic_pm_domain *genpd)
217 __releases(&genpd->lock) __acquires(&genpd->lock)
219 struct device *dev = dle->dev;
220 struct device_driver *drv = dev->driver;
222 if (!dle->need_restore)
225 mutex_unlock(&genpd->lock);
227 if (drv && drv->pm && drv->pm->runtime_resume) {
228 if (genpd->start_device)
229 genpd->start_device(dev);
231 drv->pm->runtime_resume(dev);
233 if (genpd->stop_device)
234 genpd->stop_device(dev);
237 mutex_lock(&genpd->lock);
239 dle->need_restore = false;
243 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
244 * @genpd: PM domain to check.
246 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
247 * a "power off" operation, which means that a "power on" has occured in the
248 * meantime, or if its resume_count field is different from zero, which means
249 * that one of its devices has been resumed in the meantime.
251 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
253 return genpd->status == GPD_STATE_WAIT_PARENT
254 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
258 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
259 * @genpd: PM domait to power off.
261 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
264 void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
266 if (!work_pending(&genpd->power_off_work))
267 queue_work(pm_wq, &genpd->power_off_work);
271 * pm_genpd_poweroff - Remove power from a given PM domain.
272 * @genpd: PM domain to power down.
274 * If all of the @genpd's devices have been suspended and all of its subdomains
275 * have been powered down, run the runtime suspend callbacks provided by all of
276 * the @genpd's devices' drivers and remove power from @genpd.
278 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
279 __releases(&genpd->lock) __acquires(&genpd->lock)
281 struct generic_pm_domain *parent;
282 struct dev_list_entry *dle;
283 unsigned int not_suspended;
288 * Do not try to power off the domain in the following situations:
289 * (1) The domain is already in the "power off" state.
290 * (2) The domain is waiting for its parent to power up.
291 * (3) One of the domain's devices is being resumed right now.
292 * (4) System suspend is in progress.
294 if (genpd->status == GPD_STATE_POWER_OFF
295 || genpd->status == GPD_STATE_WAIT_PARENT
296 || genpd->resume_count > 0 || genpd->prepared_count > 0)
299 if (atomic_read(&genpd->sd_count) > 0)
303 list_for_each_entry(dle, &genpd->dev_list, node)
304 if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
307 if (not_suspended > genpd->in_progress)
310 if (genpd->poweroff_task) {
312 * Another instance of pm_genpd_poweroff() is executing
313 * callbacks, so tell it to start over and return.
315 genpd->status = GPD_STATE_REPEAT;
319 if (genpd->gov && genpd->gov->power_down_ok) {
320 if (!genpd->gov->power_down_ok(&genpd->domain))
324 genpd->status = GPD_STATE_BUSY;
325 genpd->poweroff_task = current;
327 list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
328 ret = atomic_read(&genpd->sd_count) == 0 ?
329 __pm_genpd_save_device(dle, genpd) : -EBUSY;
331 if (genpd_abort_poweroff(genpd))
335 genpd_set_active(genpd);
339 if (genpd->status == GPD_STATE_REPEAT) {
340 genpd->poweroff_task = NULL;
345 if (genpd->power_off) {
346 if (atomic_read(&genpd->sd_count) > 0) {
352 * If sd_count > 0 at this point, one of the children hasn't
353 * managed to call pm_genpd_poweron() for the parent yet after
354 * incrementing it. In that case pm_genpd_poweron() will wait
355 * for us to drop the lock, so we can call .power_off() and let
356 * the pm_genpd_poweron() restore power for us (this shouldn't
357 * happen very often).
359 ret = genpd->power_off(genpd);
361 genpd_set_active(genpd);
366 genpd->status = GPD_STATE_POWER_OFF;
368 parent = genpd->parent;
369 if (parent && genpd_sd_counter_dec(parent))
370 genpd_queue_power_off_work(parent);
373 genpd->poweroff_task = NULL;
374 wake_up_all(&genpd->status_wait_queue);
379 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
380 * @work: Work structure used for scheduling the execution of this function.
382 static void genpd_power_off_work_fn(struct work_struct *work)
384 struct generic_pm_domain *genpd;
386 genpd = container_of(work, struct generic_pm_domain, power_off_work);
388 genpd_acquire_lock(genpd);
389 pm_genpd_poweroff(genpd);
390 genpd_release_lock(genpd);
394 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
395 * @dev: Device to suspend.
397 * Carry out a runtime suspend of a device under the assumption that its
398 * pm_domain field points to the domain member of an object of type
399 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
401 static int pm_genpd_runtime_suspend(struct device *dev)
403 struct generic_pm_domain *genpd;
405 dev_dbg(dev, "%s()\n", __func__);
407 genpd = dev_to_genpd(dev);
411 if (genpd->stop_device) {
412 int ret = genpd->stop_device(dev);
417 mutex_lock(&genpd->lock);
418 genpd->in_progress++;
419 pm_genpd_poweroff(genpd);
420 genpd->in_progress--;
421 mutex_unlock(&genpd->lock);
427 * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
428 * @dev: Device to resume.
429 * @genpd: PM domain the device belongs to.
431 static void __pm_genpd_runtime_resume(struct device *dev,
432 struct generic_pm_domain *genpd)
434 struct dev_list_entry *dle;
436 list_for_each_entry(dle, &genpd->dev_list, node) {
437 if (dle->dev == dev) {
438 __pm_genpd_restore_device(dle, genpd);
445 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
446 * @dev: Device to resume.
448 * Carry out a runtime resume of a device under the assumption that its
449 * pm_domain field points to the domain member of an object of type
450 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
452 static int pm_genpd_runtime_resume(struct device *dev)
454 struct generic_pm_domain *genpd;
458 dev_dbg(dev, "%s()\n", __func__);
460 genpd = dev_to_genpd(dev);
464 mutex_lock(&genpd->lock);
465 ret = __pm_genpd_poweron(genpd);
467 mutex_unlock(&genpd->lock);
470 genpd->status = GPD_STATE_BUSY;
471 genpd->resume_count++;
473 prepare_to_wait(&genpd->status_wait_queue, &wait,
474 TASK_UNINTERRUPTIBLE);
476 * If current is the powering off task, we have been called
477 * reentrantly from one of the device callbacks, so we should
480 if (!genpd->poweroff_task || genpd->poweroff_task == current)
482 mutex_unlock(&genpd->lock);
486 mutex_lock(&genpd->lock);
488 finish_wait(&genpd->status_wait_queue, &wait);
489 __pm_genpd_runtime_resume(dev, genpd);
490 genpd->resume_count--;
491 genpd_set_active(genpd);
492 wake_up_all(&genpd->status_wait_queue);
493 mutex_unlock(&genpd->lock);
495 if (genpd->start_device)
496 genpd->start_device(dev);
502 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
504 void pm_genpd_poweroff_unused(void)
506 struct generic_pm_domain *genpd;
508 mutex_lock(&gpd_list_lock);
510 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
511 genpd_queue_power_off_work(genpd);
513 mutex_unlock(&gpd_list_lock);
518 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
519 static inline void __pm_genpd_runtime_resume(struct device *dev,
520 struct generic_pm_domain *genpd) {}
522 #define pm_genpd_runtime_suspend NULL
523 #define pm_genpd_runtime_resume NULL
525 #endif /* CONFIG_PM_RUNTIME */
527 #ifdef CONFIG_PM_SLEEP
530 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
531 * @genpd: PM domain to power off, if possible.
533 * Check if the given PM domain can be powered off (during system suspend or
534 * hibernation) and do that if so. Also, in that case propagate to its parent.
536 * This function is only called in "noirq" stages of system power transitions,
537 * so it need not acquire locks (all of the "noirq" callbacks are executed
538 * sequentially, so it is guaranteed that it will never run twice in parallel).
540 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
542 struct generic_pm_domain *parent = genpd->parent;
544 if (genpd->status == GPD_STATE_POWER_OFF)
547 if (genpd->suspended_count != genpd->device_count
548 || atomic_read(&genpd->sd_count) > 0)
551 if (genpd->power_off)
552 genpd->power_off(genpd);
554 genpd->status = GPD_STATE_POWER_OFF;
556 genpd_sd_counter_dec(parent);
557 pm_genpd_sync_poweroff(parent);
562 * resume_needed - Check whether to resume a device before system suspend.
563 * @dev: Device to check.
564 * @genpd: PM domain the device belongs to.
566 * There are two cases in which a device that can wake up the system from sleep
567 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
568 * to wake up the system and it has to remain active for this purpose while the
569 * system is in the sleep state and (2) if the device is not enabled to wake up
570 * the system from sleep states and it generally doesn't generate wakeup signals
571 * by itself (those signals are generated on its behalf by other parts of the
572 * system). In the latter case it may be necessary to reconfigure the device's
573 * wakeup settings during system suspend, because it may have been set up to
574 * signal remote wakeup from the system's working state as needed by runtime PM.
575 * Return 'true' in either of the above cases.
577 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
581 if (!device_can_wakeup(dev))
584 active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
585 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
589 * pm_genpd_prepare - Start power transition of a device in a PM domain.
590 * @dev: Device to start the transition of.
592 * Start a power transition of a device (during a system-wide power transition)
593 * under the assumption that its pm_domain field points to the domain member of
594 * an object of type struct generic_pm_domain representing a PM domain
595 * consisting of I/O devices.
597 static int pm_genpd_prepare(struct device *dev)
599 struct generic_pm_domain *genpd;
602 dev_dbg(dev, "%s()\n", __func__);
604 genpd = dev_to_genpd(dev);
609 * If a wakeup request is pending for the device, it should be woken up
610 * at this point and a system wakeup event should be reported if it's
611 * set up to wake up the system from sleep states.
613 pm_runtime_get_noresume(dev);
614 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
615 pm_wakeup_event(dev, 0);
617 if (pm_wakeup_pending()) {
618 pm_runtime_put_sync(dev);
622 if (resume_needed(dev, genpd))
623 pm_runtime_resume(dev);
625 genpd_acquire_lock(genpd);
627 if (genpd->prepared_count++ == 0)
628 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
630 genpd_release_lock(genpd);
632 if (genpd->suspend_power_off) {
633 pm_runtime_put_noidle(dev);
638 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
639 * so pm_genpd_poweron() will return immediately, but if the device
640 * is suspended (e.g. it's been stopped by .stop_device()), we need
641 * to make it operational.
643 pm_runtime_resume(dev);
644 __pm_runtime_disable(dev, false);
646 ret = pm_generic_prepare(dev);
648 mutex_lock(&genpd->lock);
650 if (--genpd->prepared_count == 0)
651 genpd->suspend_power_off = false;
653 mutex_unlock(&genpd->lock);
654 pm_runtime_enable(dev);
657 pm_runtime_put_sync(dev);
662 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
663 * @dev: Device to suspend.
665 * Suspend a device under the assumption that its pm_domain field points to the
666 * domain member of an object of type struct generic_pm_domain representing
667 * a PM domain consisting of I/O devices.
669 static int pm_genpd_suspend(struct device *dev)
671 struct generic_pm_domain *genpd;
673 dev_dbg(dev, "%s()\n", __func__);
675 genpd = dev_to_genpd(dev);
679 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
683 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
684 * @dev: Device to suspend.
686 * Carry out a late suspend of a device under the assumption that its
687 * pm_domain field points to the domain member of an object of type
688 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
690 static int pm_genpd_suspend_noirq(struct device *dev)
692 struct generic_pm_domain *genpd;
695 dev_dbg(dev, "%s()\n", __func__);
697 genpd = dev_to_genpd(dev);
701 if (genpd->suspend_power_off)
704 ret = pm_generic_suspend_noirq(dev);
708 if (device_may_wakeup(dev)
709 && genpd->active_wakeup && genpd->active_wakeup(dev))
712 if (genpd->stop_device)
713 genpd->stop_device(dev);
716 * Since all of the "noirq" callbacks are executed sequentially, it is
717 * guaranteed that this function will never run twice in parallel for
718 * the same PM domain, so it is not necessary to use locking here.
720 genpd->suspended_count++;
721 pm_genpd_sync_poweroff(genpd);
727 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
728 * @dev: Device to resume.
730 * Carry out an early resume of a device under the assumption that its
731 * pm_domain field points to the domain member of an object of type
732 * struct generic_pm_domain representing a power domain consisting of I/O
735 static int pm_genpd_resume_noirq(struct device *dev)
737 struct generic_pm_domain *genpd;
739 dev_dbg(dev, "%s()\n", __func__);
741 genpd = dev_to_genpd(dev);
745 if (genpd->suspend_power_off)
749 * Since all of the "noirq" callbacks are executed sequentially, it is
750 * guaranteed that this function will never run twice in parallel for
751 * the same PM domain, so it is not necessary to use locking here.
753 pm_genpd_poweron(genpd);
754 genpd->suspended_count--;
755 if (genpd->start_device)
756 genpd->start_device(dev);
758 return pm_generic_resume_noirq(dev);
762 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
763 * @dev: Device to resume.
765 * Resume a device under the assumption that its pm_domain field points to the
766 * domain member of an object of type struct generic_pm_domain representing
767 * a power domain consisting of I/O devices.
769 static int pm_genpd_resume(struct device *dev)
771 struct generic_pm_domain *genpd;
773 dev_dbg(dev, "%s()\n", __func__);
775 genpd = dev_to_genpd(dev);
779 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
783 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
784 * @dev: Device to freeze.
786 * Freeze a device under the assumption that its pm_domain field points to the
787 * domain member of an object of type struct generic_pm_domain representing
788 * a power domain consisting of I/O devices.
790 static int pm_genpd_freeze(struct device *dev)
792 struct generic_pm_domain *genpd;
794 dev_dbg(dev, "%s()\n", __func__);
796 genpd = dev_to_genpd(dev);
800 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
804 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
805 * @dev: Device to freeze.
807 * Carry out a late freeze of a device under the assumption that its
808 * pm_domain field points to the domain member of an object of type
809 * struct generic_pm_domain representing a power domain consisting of I/O
812 static int pm_genpd_freeze_noirq(struct device *dev)
814 struct generic_pm_domain *genpd;
817 dev_dbg(dev, "%s()\n", __func__);
819 genpd = dev_to_genpd(dev);
823 if (genpd->suspend_power_off)
826 ret = pm_generic_freeze_noirq(dev);
830 if (genpd->stop_device)
831 genpd->stop_device(dev);
837 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
838 * @dev: Device to thaw.
840 * Carry out an early thaw of a device under the assumption that its
841 * pm_domain field points to the domain member of an object of type
842 * struct generic_pm_domain representing a power domain consisting of I/O
845 static int pm_genpd_thaw_noirq(struct device *dev)
847 struct generic_pm_domain *genpd;
849 dev_dbg(dev, "%s()\n", __func__);
851 genpd = dev_to_genpd(dev);
855 if (genpd->suspend_power_off)
858 if (genpd->start_device)
859 genpd->start_device(dev);
861 return pm_generic_thaw_noirq(dev);
865 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
866 * @dev: Device to thaw.
868 * Thaw a device under the assumption that its pm_domain field points to the
869 * domain member of an object of type struct generic_pm_domain representing
870 * a power domain consisting of I/O devices.
872 static int pm_genpd_thaw(struct device *dev)
874 struct generic_pm_domain *genpd;
876 dev_dbg(dev, "%s()\n", __func__);
878 genpd = dev_to_genpd(dev);
882 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
886 * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
887 * @dev: Device to suspend.
889 * Power off a device under the assumption that its pm_domain field points to
890 * the domain member of an object of type struct generic_pm_domain representing
891 * a PM domain consisting of I/O devices.
893 static int pm_genpd_dev_poweroff(struct device *dev)
895 struct generic_pm_domain *genpd;
897 dev_dbg(dev, "%s()\n", __func__);
899 genpd = dev_to_genpd(dev);
903 return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
907 * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
908 * @dev: Device to suspend.
910 * Carry out a late powering off of a device under the assumption that its
911 * pm_domain field points to the domain member of an object of type
912 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
914 static int pm_genpd_dev_poweroff_noirq(struct device *dev)
916 struct generic_pm_domain *genpd;
919 dev_dbg(dev, "%s()\n", __func__);
921 genpd = dev_to_genpd(dev);
925 if (genpd->suspend_power_off)
928 ret = pm_generic_poweroff_noirq(dev);
932 if (device_may_wakeup(dev)
933 && genpd->active_wakeup && genpd->active_wakeup(dev))
936 if (genpd->stop_device)
937 genpd->stop_device(dev);
940 * Since all of the "noirq" callbacks are executed sequentially, it is
941 * guaranteed that this function will never run twice in parallel for
942 * the same PM domain, so it is not necessary to use locking here.
944 genpd->suspended_count++;
945 pm_genpd_sync_poweroff(genpd);
951 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
952 * @dev: Device to resume.
954 * Carry out an early restore of a device under the assumption that its
955 * pm_domain field points to the domain member of an object of type
956 * struct generic_pm_domain representing a power domain consisting of I/O
959 static int pm_genpd_restore_noirq(struct device *dev)
961 struct generic_pm_domain *genpd;
963 dev_dbg(dev, "%s()\n", __func__);
965 genpd = dev_to_genpd(dev);
970 * Since all of the "noirq" callbacks are executed sequentially, it is
971 * guaranteed that this function will never run twice in parallel for
972 * the same PM domain, so it is not necessary to use locking here.
974 genpd->status = GPD_STATE_POWER_OFF;
975 if (genpd->suspend_power_off) {
977 * The boot kernel might put the domain into the power on state,
978 * so make sure it really is powered off.
980 if (genpd->power_off)
981 genpd->power_off(genpd);
985 pm_genpd_poweron(genpd);
986 genpd->suspended_count--;
987 if (genpd->start_device)
988 genpd->start_device(dev);
990 return pm_generic_restore_noirq(dev);
994 * pm_genpd_restore - Restore a device belonging to an I/O power domain.
995 * @dev: Device to resume.
997 * Restore a device under the assumption that its pm_domain field points to the
998 * domain member of an object of type struct generic_pm_domain representing
999 * a power domain consisting of I/O devices.
1001 static int pm_genpd_restore(struct device *dev)
1003 struct generic_pm_domain *genpd;
1005 dev_dbg(dev, "%s()\n", __func__);
1007 genpd = dev_to_genpd(dev);
1011 return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
1015 * pm_genpd_complete - Complete power transition of a device in a power domain.
1016 * @dev: Device to complete the transition of.
1018 * Complete a power transition of a device (during a system-wide power
1019 * transition) under the assumption that its pm_domain field points to the
1020 * domain member of an object of type struct generic_pm_domain representing
1021 * a power domain consisting of I/O devices.
1023 static void pm_genpd_complete(struct device *dev)
1025 struct generic_pm_domain *genpd;
1028 dev_dbg(dev, "%s()\n", __func__);
1030 genpd = dev_to_genpd(dev);
1034 mutex_lock(&genpd->lock);
1036 run_complete = !genpd->suspend_power_off;
1037 if (--genpd->prepared_count == 0)
1038 genpd->suspend_power_off = false;
1040 mutex_unlock(&genpd->lock);
1043 pm_generic_complete(dev);
1044 pm_runtime_set_active(dev);
1045 pm_runtime_enable(dev);
1046 pm_runtime_idle(dev);
1052 #define pm_genpd_prepare NULL
1053 #define pm_genpd_suspend NULL
1054 #define pm_genpd_suspend_noirq NULL
1055 #define pm_genpd_resume_noirq NULL
1056 #define pm_genpd_resume NULL
1057 #define pm_genpd_freeze NULL
1058 #define pm_genpd_freeze_noirq NULL
1059 #define pm_genpd_thaw_noirq NULL
1060 #define pm_genpd_thaw NULL
1061 #define pm_genpd_dev_poweroff_noirq NULL
1062 #define pm_genpd_dev_poweroff NULL
1063 #define pm_genpd_restore_noirq NULL
1064 #define pm_genpd_restore NULL
1065 #define pm_genpd_complete NULL
1067 #endif /* CONFIG_PM_SLEEP */
1070 * pm_genpd_add_device - Add a device to an I/O PM domain.
1071 * @genpd: PM domain to add the device to.
1072 * @dev: Device to be added.
1074 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1076 struct dev_list_entry *dle;
1079 dev_dbg(dev, "%s()\n", __func__);
1081 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1084 genpd_acquire_lock(genpd);
1086 if (genpd->status == GPD_STATE_POWER_OFF) {
1091 if (genpd->prepared_count > 0) {
1096 list_for_each_entry(dle, &genpd->dev_list, node)
1097 if (dle->dev == dev) {
1102 dle = kzalloc(sizeof(*dle), GFP_KERNEL);
1109 dle->need_restore = false;
1110 list_add_tail(&dle->node, &genpd->dev_list);
1111 genpd->device_count++;
1113 spin_lock_irq(&dev->power.lock);
1114 dev->pm_domain = &genpd->domain;
1115 spin_unlock_irq(&dev->power.lock);
1118 genpd_release_lock(genpd);
1124 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1125 * @genpd: PM domain to remove the device from.
1126 * @dev: Device to be removed.
1128 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1131 struct dev_list_entry *dle;
1134 dev_dbg(dev, "%s()\n", __func__);
1136 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1139 genpd_acquire_lock(genpd);
1141 if (genpd->prepared_count > 0) {
1146 list_for_each_entry(dle, &genpd->dev_list, node) {
1147 if (dle->dev != dev)
1150 spin_lock_irq(&dev->power.lock);
1151 dev->pm_domain = NULL;
1152 spin_unlock_irq(&dev->power.lock);
1154 genpd->device_count--;
1155 list_del(&dle->node);
1163 genpd_release_lock(genpd);
1169 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1170 * @genpd: Master PM domain to add the subdomain to.
1171 * @new_subdomain: Subdomain to be added.
1173 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1174 struct generic_pm_domain *new_subdomain)
1176 struct generic_pm_domain *subdomain;
1179 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
1183 genpd_acquire_lock(genpd);
1184 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
1186 if (new_subdomain->status != GPD_STATE_POWER_OFF
1187 && new_subdomain->status != GPD_STATE_ACTIVE) {
1188 mutex_unlock(&new_subdomain->lock);
1189 genpd_release_lock(genpd);
1193 if (genpd->status == GPD_STATE_POWER_OFF
1194 && new_subdomain->status != GPD_STATE_POWER_OFF) {
1199 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1200 if (subdomain == new_subdomain) {
1206 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
1207 new_subdomain->parent = genpd;
1208 if (subdomain->status != GPD_STATE_POWER_OFF)
1209 genpd_sd_counter_inc(genpd);
1212 mutex_unlock(&new_subdomain->lock);
1213 genpd_release_lock(genpd);
1219 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1220 * @genpd: Master PM domain to remove the subdomain from.
1221 * @target: Subdomain to be removed.
1223 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1224 struct generic_pm_domain *target)
1226 struct generic_pm_domain *subdomain;
1229 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
1233 genpd_acquire_lock(genpd);
1235 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1236 if (subdomain != target)
1239 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1241 if (subdomain->status != GPD_STATE_POWER_OFF
1242 && subdomain->status != GPD_STATE_ACTIVE) {
1243 mutex_unlock(&subdomain->lock);
1244 genpd_release_lock(genpd);
1248 list_del(&subdomain->sd_node);
1249 subdomain->parent = NULL;
1250 if (subdomain->status != GPD_STATE_POWER_OFF)
1251 genpd_sd_counter_dec(genpd);
1253 mutex_unlock(&subdomain->lock);
1259 genpd_release_lock(genpd);
1265 * pm_genpd_init - Initialize a generic I/O PM domain object.
1266 * @genpd: PM domain object to initialize.
1267 * @gov: PM domain governor to associate with the domain (may be NULL).
1268 * @is_off: Initial value of the domain's power_is_off field.
1270 void pm_genpd_init(struct generic_pm_domain *genpd,
1271 struct dev_power_governor *gov, bool is_off)
1273 if (IS_ERR_OR_NULL(genpd))
1276 INIT_LIST_HEAD(&genpd->sd_node);
1277 genpd->parent = NULL;
1278 INIT_LIST_HEAD(&genpd->dev_list);
1279 INIT_LIST_HEAD(&genpd->sd_list);
1280 mutex_init(&genpd->lock);
1282 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1283 genpd->in_progress = 0;
1284 atomic_set(&genpd->sd_count, 0);
1285 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1286 init_waitqueue_head(&genpd->status_wait_queue);
1287 genpd->poweroff_task = NULL;
1288 genpd->resume_count = 0;
1289 genpd->device_count = 0;
1290 genpd->suspended_count = 0;
1291 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1292 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1293 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
1294 genpd->domain.ops.prepare = pm_genpd_prepare;
1295 genpd->domain.ops.suspend = pm_genpd_suspend;
1296 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1297 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1298 genpd->domain.ops.resume = pm_genpd_resume;
1299 genpd->domain.ops.freeze = pm_genpd_freeze;
1300 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1301 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1302 genpd->domain.ops.thaw = pm_genpd_thaw;
1303 genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1304 genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1305 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1306 genpd->domain.ops.restore = pm_genpd_restore;
1307 genpd->domain.ops.complete = pm_genpd_complete;
1308 mutex_lock(&gpd_list_lock);
1309 list_add(&genpd->gpd_list_node, &gpd_list);
1310 mutex_unlock(&gpd_list_lock);